Linq query fails on ToList(): 'object reference not set to an instance of object' RRS feed

  • Question

  • Hello,

    I have this linq query:

    var result = from p in context.my_query select p;
    var result_list = result.ToList();
    //    OR
    var result = from p on context.my_query.ToList() select p;

    I get an the following error when try to execute it:

    2018-01-10 07:29:39,201 [14] ERROR UCDLogger System.NullReferenceException: Object reference not set to an instance of an object.
       at System.Data.Entity.Core.EntityKey.AddHashValue(Int32 hashCode, Object keyValue)
       at System.Data.Entity.Core.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.Entity.Core.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key, EntityEntry& entry)
       at System.Data.Entity.Core.Objects.ObjectStateManager.FindEntityEntry(EntityKey key)
       at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
       at lambda_method(Closure , Shaper )
       at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
       at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

    I don't know what is actually null since in my entity, there are only types such as strings, float and DateTime. I would also like to precise that the Double and DateTime attributes are set to Nullable.

    How could I solve this problem?


    What I've tried so far:
    1- context is not null
    2- my_query is not null
    3- result.Count() is greater than 0
    4- If I do the following:

    var results = from p in context.my_query select p;
    foreach(var t in results)

    The loop enters the first iteration and displays the type of t, but on the second iteration it throws the exception.


    • Edited by Imad93 Wednesday, January 10, 2018 7:37 AM
    Wednesday, January 10, 2018 6:58 AM

All replies

  • Check that context and my_query are not null.

    Wednesday, January 10, 2018 7:18 AM
  • Neither is null. Please check my edit to see what I have tried so far.
    Wednesday, January 10, 2018 7:38 AM
  • Is it possible there is an issue somewhere deeper in your code?

    That is, I don't know what kind of objects context.my_query is returning, but somewhere in my_query must be some kind of an enumerator. Perhaps this enumerator is yielding return values and accessing a property that it expects to be non-null?

    Wednesday, January 10, 2018 8:27 AM
  • As I described before, my_query contains only Double, string and DateTime values and all Double and DateTime values are set to nullable in my entity.

    So context.my_query.ToList() will return a List<my_query>.

    And since the count is greater than zero and that the exception is thrown on the second iteration, maybe the second object in the list is, for some unknown reasons, not instantiated.

    And I am wondering if this problem is because of the database (maybe it is returning some invalid value or something like that?)

    Wednesday, January 10, 2018 9:46 AM
  • Did you make data nullable from the beginning, or it was a recent change?

    Wednesday, January 10, 2018 12:16 PM
  • I made it from the beginning
    Wednesday, January 10, 2018 1:50 PM