-2

I have the below code:

using System;
using System.Collections.Generic;

public class XTRAsystem
{
    public string version
    { get; set; }

    public XTRAapi api
    { get; set; }

    public XTRAsystem(string system_version)
    {
        this.version = system_version;
        XTRAapi api = new XTRAapi(Guid.NewGuid());
        Console.WriteLine("New XTRA system (" + this.version + ") initialized.");
    }

    public class XTRAapi
    {
        public Guid id
        { get; set; }

        public string name
        { get; set; }

        public XTRAapi(Guid Id)
        {
            this.id = Id;
            Console.WriteLine("New T24 API created.");
        }
    }
}

public class testCase
{
    public int caseNumber
    { get; set; }

    public List<string> data;

    public XTRAsystem refSystem
    { get; set; }

    public testCase()
    {
        data = new List<string>();
        Console.WriteLine("New Test Case created.");
    }
}

There is a reason that the two classes above are nested. Now, on my Main, in Program, the below code produces Null reference exception. Could someone help me?

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Start..");

            XTRAsystem mySystem = new XTRAsystem("Mickey");
            testCase[] myTest = new testCase[100];

            Console.WriteLine("Capacity is {0}", myTest.Length);

            //  'Object reference not set to an instance of an object' for all the below
            myTest[0].caseNumber = 1;

            myTest[0].data.Add("first test");
            myTest[0].data.Add("second test");
            myTest[0].data.Add("third test");

            myTest[0].refSystem = mySystem;
        }
    }
}

By your expertise and experience, is there any other way to produce the functionality that now produces error (line myTest[0]... etc.) Thank you very much

Nick
  • 483
  • 1
  • 6
  • 15

1 Answers1

1

You have to initialize each of your array item before you can use the properties

testCase[] myTest = new testCase[100];

Console.WriteLine("Capacity is {0}", myTest.Length);
myTest[0] = new testCase();
           
myTest[0].caseNumber = 1;

// also, initialize your list
myTest[0].data = new List<string>();
Jawad
  • 11,028
  • 3
  • 24
  • 37
  • Thank you very much! One last thing: what's the purpose of initializing the data List in the testCase constructor `data = new List();`, if I have to initialize it (again) outside for each new testCase? – Nick May 25 '21 at 21:27
  • 1
    Creating a type and initializing the variable are two different things. You can initialize the data array within the class but it has to be done. In your testCase class, change the data initialization to this, `public List data = new List();`. This way its initialized when you initialize the array element. – Jawad May 25 '21 at 21:34
  • Thank you so much for your time and valued input. I will do as you suggest, and remove the line from the constructor, has no meaning. Much appreciated ! – Nick May 25 '21 at 21:43
  • 1
    Actually, a note: if I initialize next to the property (`public Some thing {get; set;} = new Some();`) and then ALSO initialize in the constructor of the class, the `thing` is being initialized twice.. So, someone should initialize *either* after the property, *or* within the constructor, not both times.. Thank you for displaying this concept. – Nick May 26 '21 at 09:32
  • 1
    Correct. You only need to Initialize Lists if they are not initialized in constructors. – Jawad May 26 '21 at 12:26