title: "Object reference not set to an instance of an object" error when trying to add objects to a list inside a class
body: <p>I have some custom classes defined that include lists of other classes like so:</p>
  
  <pre><code>public class Unit
  {
      public string Name { get; set; }
      public List&lt;Group&gt; 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 };
  </code></pre>
  
  <p>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.</p>
  
  <p>I then try to start building out these classes like so:</p>
  
  <pre><code>Unit unit1 = new Unit() { Name = "Unit 1" };
  Group group1 = new Group() { Name = "Group 1", Number = 10, Type = basic_1 };
  unit1.Contains.Add(group1);
  </code></pre>
  
  <p>But here I receive the error (on the <code>Add</code> method) "Object reference not set to an instance of an object." Looking at the locals in the debugger I can see that the <code>Unit</code>, <code>Group</code> and <code>Type</code> were all created successfully and the <code>group1</code> contains all the <code>Type</code> values it's supposed to, but <code>unit1.Contains</code> is null.</p>
  
  <p>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.</p>