title: Why am I getting a NullReferenceException in System.Data.Entity.dll (Entity Framework)?
body: <p>I have a <code>List&lt;State&gt;</code> of <code>State</code> entity objects, and each <code>State</code> object has collections of other objects, such as <code>Licenses</code>, <code>Taxes</code>, <code>Bonds</code>, etc</p>
  
  <p>There's also a collection for <code>ExpiredObjects</code>, which is a list of any object which is expired and needs to be renewed. For some reason, this property is giving me a <code>NullReferenceException</code> when I try and access it for one specific state (Nevada), but I can't for the life of me figure out what is actually <code>null</code> since I don't see any <code>null</code> values anywhere.</p>
  
  <p>Here's my code that throws the exception. It loops through all the states, and adds all the <code>ExpiredObjects</code> to a view-specific collection which gets displayed. My test code is still included</p>
  
  <pre><code>    private List&lt;ExpiredObject&gt; LoadAllExpiredObjects()
      {
          var list = new List&lt;ExpiredObject&gt;();
          foreach (var state in States)
          {
              // This tells me the state is not null, and neither is state.ExpiredObjects
              Debug.WriteLine(string.Format("{0}|{1}|{2}", 
                  state.Name, state == null, state.ExpiredObjects == null));
  
              try
              {
                  var x = state.ExpiredObjects;
                  Debug.WriteLine(x == null);
  
                  // Exception occurs on the "for" line on the Nevada state only
                  // Contents in for loop do not execute
                  foreach (var item in x)
                  {
                      Debug.WriteLine(string.Format("{0}", item));
                      list.Add(item);
                  }
  
                  // This used to be the only line of code before I started testing
                  // It fails on Nevada
                  //list.AddRange(state.ExpiredObjects);
              }
              catch (Exception ex)
              {
                  Debug.WriteLine(ex.Message);
                  Debug.WriteLine(ex.StackTrace);
              }
          }
          return list;
      }
  </code></pre>
  
  <p>The stack trace of the error is this:</p>
  
  <pre><code>A first chance exception of type 'System.NullReferenceException' occurred in System.Data.Entity.dll
  Object reference not set to an instance of an object.
     at System.Data.EntityKey.AddHashValue(Int32 hashCode, Object keyValue)
     at System.Data.EntityKey.GetHashCode()
     at System.Collections.Generic.GenericEqualityComparer`1.GetHashCode(T obj)
     at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
     at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue&amp; value)
     at System.Data.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key, EntityEntry&amp; entry)
     at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
     at lambda_method(Closure , Shaper )
     at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
     at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
     at System.Data.Objects.DataClasses.RelatedEnd.Merge[TEntity](IEnumerable`1 collection, MergeOption mergeOption, Boolean setIsLoaded)
     at System.Data.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption)
     at System.Data.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption)
     at System.Data.Objects.DataClasses.RelatedEnd.Load()
     at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad()
     at System.Data.Objects.DataClasses.EntityCollection`1.GetEnumerator()
     at MyNamespace.StatesViewModel.LoadAllExpiredObjects() in C:\Users\me\Desktop\MySolution\StatesViewModel.cs:line 217
  </code></pre>
  
  <p>I also get the exact same error when I select Nevada and it tries to bind a <code>DataGrid</code> to the <code>ExpiredObjects</code> collection (if I comment out that binding, the code works fine)</p>
  
  <p>Does anyone know what could be causing this error?</p>