0

I have created some nested classes but don't unterstand how to set the variables in the classes. My code so far only gives me a error:

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

Code:

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];
        }

    }
}

The error is at this line of code:

table.field[0].fieldName = fieldN[0];
brachi
  • 119
  • 1
  • 3
  • 16
  • Possible duplicate of https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Wheels73 Jul 19 '18 at 07:19
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mjwills Jul 19 '18 at 07:20
  • 1
    Do you mean `fieldN.Count` instead of `fieldN.Capacity`? – vc 74 Jul 19 '18 at 07:22

1 Answers1

3

You have only initialized the array table.field, not the ITEMS in the array. You need to initialize each item before you can access its members:

for(int i=0; i<table.field.Length; i++)
    table.field[i] = new Feld();
M Kloster
  • 679
  • 4
  • 13