0

structで構造体配列を使用するとエラーが発生するのはなぜですか?

public struct Tables
{
    public string name;
}

public struct Schema
{
    public string name;
    public Tables[] tables; //Declarate struct array in struct
}

このコードで構造体を使用しました:

Schema schema;

private void Form1_Load(object sender, EventArgs e)
{
    schema.name = "schemaName";
    schema.tables[0].name = "tables1Name"; // Error in here: Object reference not set to an instance of an object
}
4

2 に答える 2

2

配列を初期化する必要があります。

schema.name = "schemaName";
schema.tables = new Tables[10];
schema.tables[0].name = "tables1";
于 2012-11-15T22:53:10.167 に答える
2

初期化されていないためschema.tables、null です。

試す

private void Form1_Load(object sender, EventArgs e)
{
    schema.name = "schemaName";
    schema.tables = new Tables[5];
    schema.tables[0].name = "tables1"; // Error ini here: Object reference not set to an instance of an object
}
于 2012-11-15T22:54:11.297 に答える