Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7863533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:30:43+00:00 2026-06-02T23:30:43+00:00

I have a List<State> of State entity objects, and each State object has collections

  • 0

I have a List<State> of State entity objects, and each State object has collections of other objects, such as Licenses, Taxes, Bonds, etc

There’s also a collection for ExpiredObjects, which is a list of any object which is expired and needs to be renewed. For some reason, this property is giving me a NullReferenceException 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 null since I don’t see any null values anywhere.

Here’s my code that throws the exception. It loops through all the states, and adds all the ExpiredObjects to a view-specific collection which gets displayed. My test code is still included

    private List<ExpiredObject> LoadAllExpiredObjects()
    {
        var list = new List<ExpiredObject>();
        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;
    }

The stack trace of the error is this:

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& value)
   at System.Data.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key, EntityEntry& 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

I also get the exact same error when I select Nevada and it tries to bind a DataGrid to the ExpiredObjects collection (if I comment out that binding, the code works fine)

Does anyone know what could be causing this error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:30:46+00:00Added an answer on June 2, 2026 at 11:30 pm

    If it’s only for Nevada then it must be a data problem, do a thorough check in the db.

    And to summarize, the core problem was:

    • It was db-first.
    • … was returning a null value for one of the fields that was marked in EF as not null
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of lists that looks like this: x[[state]][[year]] . Each element
I have a list of states, each with some data associated with it, displayed
Right now, I have a domain entity named StyleBundle. This StyleBundle takes a list
i have a US states list List<string> state // contain all 51 US states
I have a whole list of entity classes which I need to make Serializable
Scenario : A Customer entity has properties for State and Color . i.e. Washington
I'm trying to get a list of entity types that have been persisted as
I have a list of States in an ItemsControl, the DataTemplate is a CheckBox.
I have a list component on the stage in an AS3 movie. I populate
I have a list of cities, states/provinces, and countries and I need to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.