简体   繁体   中英

Partial Class Instance Initialization Null Reference Exception

I have a service with 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 {
        get {
            return this.counternoField;
        }
        set {
            this.counternoField = value;
            this.RaisePropertyChanged("counterno");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.SoapElementAttribute("active-km")]
    public double activekm {
        get {
            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 {
        get {
            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 I want to create an instance of "ArrayOfReceipt" or set a value, or access it, I always encounter the same problem: System.NullReferenceException. This is the code when I try to 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 is always null, and I cannot initialize it. Please show me a way to initialize and set some value to this object. Thanks in advance.

The way your code is written, you can initialize prev_ConsArr.item like this:

prev_ConsArr.item = new Receipt[3];

That would create a new ArrayOfReceipt that could hold three Receipt objects. You could also create a constructor for your ArrayOfReceipt class that initializes item . Either of these methods will eliminate your NullReferenceException

Looking at the way you are using your ArrayOfReceipt class, you may want to consider changing the type of item to List<Receipt> . That would make it easier to change the number of Receipt classes you are storing.

Depending on what you are trying to do, you may also want to create an AddReceipt method in ArrayOfReceipts and move your PropertyChanged event to that method. Right now, the PropertyChanged event will only fire when your ArrayOfReceipts class overwrites its array of Receipts .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM