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.recordsifobjnullrecords.where(...)ifrecordsnullx.idin lambda if therenullentry inobj.recordsenumerable (iexin contextnull).
here wouldn't (assuming compiles):
singleordefaultthrow exception if there more one entry. if there isn't, returnnullreference type, or default value value type.(record)casting. since there no compiler error, assumerecordclass. if there value, throw exception if it's invalid cast (and not null exception).(record)nullvalid reference type.
you should debug , step through find out causing exception.
Comments
Post a Comment