I have some custom classes defined that include lists of other classes like so:
public class Unit
{
public string Name { get; set; }
public List<Group> Contains { get; set; }
}
public class Group
{
public string Name { get; set; }
public Type Type { get; set; }
public int Number { get; set; }
}
public static Type basic_1 = new Type() { Name = "basic_1", Number = 1, Cost = 13 };
The basic idea is that you have a unit which contains a list of groups, each with a different type. The type contains specific properties while the classes that contain them are organizational in nature.
I then try to start building out these classes like so:
Unit unit1 = new Unit() { Name = "Unit 1" };
Group group1 = new Group() { Name = "Group 1", Number = 10, Type = basic_1 };
unit1.Contains.Add(group1);
But here I receive the error (on the Add
method) "Object reference not set to an instance of an object." Looking at the locals in the debugger I can see that the Unit
, Group
and Type
were all created successfully and the group1
contains all the Type
values it's supposed to, but unit1.Contains
is null.
What am I doing wrong? FYI I've never done something like this before so I don't even know if it's possible, but it seems to work fine up until this point.