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.