c# - Object reference not set to an instance of an object with class -
i have situation have defined class within class, resembling following:
public partial class proddata { private string prodid; private string description; ...and on until private proddatachildren[] childitemsfield; } proddatachildren it's own class, using identify sub-products may belong parent product, hence array.
in main part of program have loop reads in records, sort of like:
while (myreader.read()) { proddata productdatain = new proddata(); productdatain.id = "values assigned here"; until point there want assign child, getting error "object reference not set instance of object", using following statement
productdatain.childitems[i].prodid = "string variable here"; } i beleive cause of error current childitems being set null, how supposed assign value it? need instantiate , instance of child item somehow , how this?
any appreciated, realize might no brainer some.
first of all, have initialize field before assigning collection. suggest using list instead of array:
public partial class proddata { ...and on until private list<proddatachildren> childitemsfield = new list<proddatachildren>(); } with list you'll able following:
productdatain.childitems.add(new proddatachildren() { prodid = "string variable here" });
Comments
Post a Comment