-1

In my code:

decimal maxPrice = list.Max(i => i.price);

Getting error - Object reference not set to an instance of an object. NullReferenceException was unhandled by code.

The i value becomes null, though the list count is 6709. How do I resolve this?

user1254053
  • 755
  • 3
  • 19
  • 55

1 Answers1

2

So your list contains nulls.

Either filter them: list.Where(l => l != null).Max(...), or prevent the nulls to end up in the list in the first place.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272