-1

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.

CJH
  • 929
  • 11
  • 32
  • 2
    Did you check all 22 elements of the original list? Do they all have the `Name` in place? – Imantas Jan 12 '19 at 09:55
  • It seems you should dereference originalList with indexes since it is a list of collections of ItemData, right? So you may try with **originalList[index]**.Where(). – Angel_D Jan 12 '19 at 10:01
  • I think you are onto something... Looks like someone has been fudging about with my ComponentTypes in the database! Thanks for that! :) – CJH Jan 12 '19 at 10:02
  • 2
    Could you please add the code as text rather than images? – mjwills Jan 12 '19 at 10:49
  • Are you sure the NullReferenceException occurs in the lamba expression? Change you debugger exception settings to break whenever a NullReferenceException occurs and you will see. – Klaus Gütter Jan 12 '19 at 14:57
  • I've closed this as a duplicate because there is nothing special or unique to distinguish it from any other null reference exception. – slugster Jan 12 '19 at 23:49

1 Answers1

1

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()});
  • Turns out someone had changed some IDs in the database this is sourced from and there were nulls in a couple of the rows! Should have checked in the first place but all sorted now. Thanks! – CJH Jan 12 '19 at 15:06