c# - Partial Class Instance Initialization Null Reference Exception -
i have service reference code below:
[system.xml.serialization.soaptypeattribute(namespace="urn:customer")] public partial class receipt : object, system.componentmodel.inotifypropertychanged { private int counternofield; private double activekwhfield; /// <remarks/> [system.xml.serialization.soapelementattribute("counter-no")] public int counterno { { return this.counternofield; } set { this.counternofield = value; this.raisepropertychanged("counterno"); } } /// <remarks/> [system.xml.serialization.soapelementattribute("active-km")] public double activekm { { return this.activekm field; } set { this.activekmfield = value; this.raisepropertychanged("activekm"); } } public event system.componentmodel.propertychangedeventhandler propertychanged; protected void raisepropertychanged(string propertyname) { system.componentmodel.propertychangedeventhandler propertychanged = this.propertychanged; if ((propertychanged != null)) { propertychanged(this, new system.componentmodel.propertychangedeventargs(propertyname)); } } } [system.xml.serialization.soaptypeattribute(namespace="urn:customer")] public partial class arrayofreceipt : object, system.componentmodel.inotifypropertychanged { private receipt[] itemfield; /// <remarks/> public receipt[] item { { return this.itemfield; } set { this.itemfield = value; this.raisepropertychanged("item"); } } public event system.componentmodel.propertychangedeventhandler propertychanged; protected void raisepropertychanged(string propertyname) { system.componentmodel.propertychangedeventhandler propertychanged = this.propertychanged; if ((propertychanged != null)) { propertychanged(this, new system.componentmodel.propertychangedeventargs(propertyname)); } } }
and, when want create instance of "arrayofreceipt" or set value, or access it, encounter same problem: system.nullreferenceexception. code when try create:
var prev_cons = new myservice.receipt(); prev_cons.counterno = 1; prev_cons.activekm = 3265; myservice.arrayofreceipt prev_consarr = new myservice.arrayofreceipt(); prev_consarr.item.setvalue(prev_cons, 0);
unfortunatelly, prev_consarr.item null, , cannot initialize it. please show me way initialize , set value object. in advance.
the way code written, can initialize prev_consarr.item this:
prev_consarr.item = new receipt[3];
that create new arrayofreceipt hold 3 receipt objects. create constructor arrayofreceipt class initializes item
. either of these methods eliminate nullreferenceexception
looking @ way using arrayofreceipt class, may want consider changing type of item
list<receipt>
. make easier change number of receipt classes storing.
depending on trying do, may want create addreceipt
method in arrayofreceipts , move yourpropertychanged
event method. right now, propertychanged
event fire when arrayofreceipts
class overwrites array of receipts
.
Comments
Post a Comment