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
):
obj.records
ifobj
null
records.where(...)
ifrecords
null
x.id
in lambda if therenull
entry inobj.records
enumerable (iex
in contextnull
).
here wouldn't (assuming compiles):
singleordefault
throw exception if there more one entry. if there isn't, returnnull
reference type, or default value value type.(record)
casting. since there no compiler error, assumerecord
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
Post a Comment