-1

I have this model that I am trying to reference and get the an array of this type.

public class TestModel
{
    public string Name { get; set; }
    public Guid Id { get; set; }
    public List<Conditions> Conditions { get; set; }
}

I also need to get the conditions for this which is a separate model with a few arrays.

public class Conditions
{
    public List<string> Country { get; set; }
    public List<int> Grades { get; set; }
    public List<string> Disciplines { get; set; }
}

I can get the Name and Id easily enough but when I try and get the values in any of the arrays I get an error Object reference not set to an instance of an object. which it would normally give as the array is not instantiated. Is there any way to do this without instantiating the array?

Code I am using to get the Id

private static ArrayList GetTests()
{
    Console.WriteLine("Get tests");

    foreach (TestModel test in testModel)
    {
        var conditions = test.Conditions.Disciplines;
        Console.WriteLine("");
        Console.WriteLine("testID: " + test.Id);
    }

    return networks;
}

The model is populated in the main method:

private static IEnumerable<TestModel> testModel= new TestModel[] { };
public static void Main(string[] args)
{
    Console.WriteLine("Start");
    Console.WriteLine("Load Test Data");
    using (var r = new StreamReader(@"C:\Production\test.json"))
    {
        string json = r.ReadToEnd();
        testModel = JsonConvert.DeserializeObject<TestModel[]>(json);
    }
    GetTests();
    Console.WriteLine("End");
    Console.Read();
}

I feel like this should be populated when the Json file is read and put into the model.

Saml92
  • 91
  • 10
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Renatas M. Jan 20 '17 at 11:10
  • Wouldn't think so I know it's a null reference exception I'm just looking to see if there is a way to do it in this situation without instantiating all of the arrays for each class that will use this model. – Saml92 Jan 20 '17 at 11:12
  • To use an object you must instantiate it, how can you use something that hasn't been created. To put it another way, you can't eat a cake before it has been made. – TheLethalCoder Jan 20 '17 at 11:13
  • As @Reniuz said you can use C# 6 features to instantiate it inline, instantiate it in the constructor or use backing fields where they are instantiated. Either way you must instantiate the object to use it. – TheLethalCoder Jan 20 '17 at 11:14
  • Also the code you have provided has nothing to do with the error you are getting, if you want people to help show the actual code giving the error. However, it will still be a duplicate. – TheLethalCoder Jan 20 '17 at 11:17

2 Answers2

1

As you said: You did not instantiate the arrays.

You could for example do it in the constructor, use the setters or simply do it inline (C# 6.0 required):

public class Conditions
{
    public string[] Country { get; set; } = new string[length];
    public int[] Grades { get; set; } = new int[length];
    public string[] Disciplines { get; set; } = new string[length];
}

"length" is the length of the array. If you don't know the size, you might consider using a List<> or something like that.

Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42
Thomas D.
  • 1,031
  • 6
  • 17
0

Switching to List for each of the properties worked. I shall edit the code above with the correct answer, Thanks to @Thomas D. for pointing me in that direction

Saml92
  • 91
  • 10