-1

Blank Array Causing System.NullReferenceException: 'Object reference not set to an instance of an object.'

So I have two classes:

    internal class Stat
    {
        public int index { get; set; }
        public string StatAb { get; set; }
        public string L0 { get; set; }
        public string L1 { get; set; }
        public string L2 { get; set; }
        public string L3 { get; set; }
        public string L4 { get; set; }
    }
    internal class Num
    {
        public int index { get; set; }
        public int StatIndex { get; set; }
        public string StatG { get; set; }
        public string Date { get; set; }
        public string DE { get; set; }
        public string Num { get; set; }
    }

references:

        List<Stat> stat = new List<Stat>();
        List<Num>[] num = new List<Num>[45];

I'm trying to turn num into an array

this is now having an error saying numbers[0] is null (it doesn't show the fields)

num[0].Add(new Num()
                        {
                            index = Convert.ToInt32(reader[0]),
                            StatIndex = Convert.ToInt32(reader[1]),
                            StatG = reader[2].ToString(),
                            Date = reader[3].ToString(),
                            DE = reader[5].ToString(),
                            Num = reader[4].ToString(),
                        });

when I type this it seems to work (it can use the StatG when I type it)

num[5][3].StateG

, but error trying to add

Josh
  • 21
  • 3

1 Answers1

0

Of course it's null. You never put anything into it so how could you get anything out of it? Think of an array as a group of variables. Each element in the array is basically a variable. If you declared a variable of type List<Num>, would you expect it to be null by default or automatically contain an object of type List<Num>? The fact that you have this:

List<Stat> stat = new List<Stat>();

demonstrates that you know it would be null by default and that you have to actually assign an object to it in order for it to not be null. Every element in your array is the same. Until you assign a List<Num> object to an element, that element is null.

If you want to avoid having to create 45 separate objects individually, you can do something like this:

List<Num>[] num = Enumerable.Range(1, 45).Select(n => new List<Num>()).ToArray()
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • your statement is confusing... I'm using the same method for the List stat.Add(new Stat() { index = Convert.ToInt32(reader[0]), ... }); – Josh May 28 '23 at 02:06
  • @Josh, no, you are not using the same method. Your `stat` variable is NOT an array type. You are creating a `List` object and assigning it to the `stat` variable. In the case of the `num` variable, you are creating the array object but you are not creating the `List` objects to put in the array. The variable itself is not `null` - the array exists - but each element in the array is `null`. It's like you have got an egg carton and are expecting to be able to make an omelette without ever putting any eggs in it. The carton exists but each cup in the carton is empty. – jmcilhinney May 28 '23 at 02:09
  • jmcilhinney, thank you, that makes much more sense. I did the Enumerable code you gave me and it worked like a charm. Thanks again. – Josh May 28 '23 at 02:13