Search code examples
c#lazy-loadingobject-reference

Accessing a Lazy-Loaded field... Object reference not set to an instance of an object


I am sure I am just missing something basic so hope I can get my question across clearly.

I have a class named ItemData which implements an interface IITemData. Within the class ItemData I have the following:

enter image description here

However when I am trying to use this ItemData class (via the interface) it looks like this ComponentType is loading okay:

enter image description here

But when I am trying to access it in a lambda expression:

enter image description here I keep getting the Object Reference error from the above.

enter image description here

I have the feeling I am just missing something straight forward.


Solution

  • Try it with null check maybe, seems like some of the data in list are null

    var sortedList = new List<IItemData>(new ItemData[]{originalList.Where(x => x?.ComponentType?.Name == "template").SingleOrDefault()});