尝试在嵌套 类 中设置数据时出错

Getting an error when trying to set data in nested classes

我创建了一些嵌套的 类,但不明白如何在 类 中设置变量。到目前为止,我的代码只给我一个错误:

System.NullReferenceException: Object reference not set to an instance of an object

代码:

class Felddaten
{
    public string data;
}

class Feld
{
    public string fieldName;
    public Felddaten[] fieldData;
}

class Tabelle
{
    public string tableName;
    public Feld[] field;
}

class Program
{
    static void Main(string[] args)
    {
        Tabelle table = new Tabelle();
        table.tableName = "T100";

        RFCConnector connector = new RFCConnector();

        connector.getFieldNames(table.tableName, out List<string> fieldN);

        table.field = new Feld[fieldN.Capacity];

        for (int i = 0; i < fieldN.Capacity; i++)
        {
            table.field[0].fieldName = fieldN[0];
        }

    }
}

错误在这行代码:

table.field[0].fieldName = fieldN[0];

您只初始化了数组table.field,没有初始化数组中的ITEMS。您需要先初始化每个项目,然后才能访问其成员:

for(int i=0; i<table.field.Length; i++)
    table.field[i] = new Feld();