c# - How to remove the null value exception in the following statement -


the following code raising null value exception

record rec = (record)obj.records.where(x => x.id == no).singleordefault(); 

there 3 potential places nullreferenceexception occur (edit: know id int):

  1. obj.records if obj null
  2. records.where(...) if records null
  3. x.id in lambda if there null entry in obj.records enumerable (ie x in context null).

here wouldn't (assuming compiles):

  1. singleordefault throw exception if there more one entry. if there isn't, return null reference type, or default value value type.
  2. (record) casting. since there no compiler error, assume record class. if there value, throw exception if it's invalid cast (and not null exception). (record)null valid reference type.

you should debug , step through find out causing exception.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -