c# - Value cannot be null. Error -
i have error method below trying add data combobox
datagrid
, after add data want calculate total amount of 'itemsellingprice' , display amount in label.
using (truckserviceclient tsc = new truckserviceclient()) { var item = cmbaddextras.selecteditem extradisplayitems; if (item != null) { var displayitem = new list<extradisplayitems> { new extradisplayitems { displayitems = item.displayitems, itemid = item.itemid, itemcode = item.itemcode, itemdescription = item.itemdescription, itemsellingprice = item.itemsellingprice, } }; dgaddextras.items.add(item); var subtotalextras = item.displayitems.sum(x => x.itemsellingprice.getvalueordefault(0)); //here lblsubtotalextrasamount.content = "r" + subtotalextras; } }
the error is:
value cannot null.
does have ideas why happening?
edit: set displayitems in class
public class extradisplayitems { public list<extradisplayitems> displayitems; public int itemid { get; set; } public string itemcode { get; set; } public string itemdescription { get; set; } public double? itemsellingprice { get; set; } }
thanks gert arnold, actual issue item.displayitems
null
(so not inside lambda, since works fine).
some proof. gets exact error message:
double?[] x = null; double? sum = x.sum(y => y);
(the reason don't nullreferenceexception
call static method (an extension method), technically reference isn't null argument is).
you should check set item.displayitems
or prevent calling sum
on item.displayitems
value null
.
Comments
Post a Comment