部分 Class 实例初始化空引用异常
Partial Class Instance Initialization Null Reference Exception
我有一个参考代码如下的服务:
[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));
}
}
}
而且,当我想创建 "ArrayOfReceipt" 的实例或设置一个值或访问它时,我总是遇到同样的问题:System.NullReferenceException。
这是我尝试创建时的代码:
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);
不幸的是,prev_ConsArr.item 总是空的,我无法初始化它。请告诉我一种初始化并为此对象设置一些值的方法。提前致谢。
您的代码编写方式,您可以像这样初始化prev_ConsArr.item:
prev_ConsArr.item = new Receipt[3];
这将创建一个可以容纳三个 Receipt 对象的新 ArrayOfReceipt。您还可以为 ArrayOfReceipt class 创建一个构造函数来初始化 item
。这些方法中的任何一种都将消除您的 NullReferenceException
查看您使用 ArrayOfReceipt class 的方式,您可能需要考虑将 item
的类型更改为 List<Receipt>
。这样可以更轻松地更改您存储的收据 classes 的数量。
根据您尝试执行的操作,您可能还想在 ArrayOfReceipts 中创建一个 AddReceipt
方法并将您的 PropertyChanged
事件移至该方法。现在,PropertyChanged
事件只会在您的 ArrayOfReceipts
class 覆盖其数组 Receipts
.
时触发
我有一个参考代码如下的服务:
[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));
}
}
}
而且,当我想创建 "ArrayOfReceipt" 的实例或设置一个值或访问它时,我总是遇到同样的问题:System.NullReferenceException。 这是我尝试创建时的代码:
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);
不幸的是,prev_ConsArr.item 总是空的,我无法初始化它。请告诉我一种初始化并为此对象设置一些值的方法。提前致谢。
您的代码编写方式,您可以像这样初始化prev_ConsArr.item:
prev_ConsArr.item = new Receipt[3];
这将创建一个可以容纳三个 Receipt 对象的新 ArrayOfReceipt。您还可以为 ArrayOfReceipt class 创建一个构造函数来初始化 item
。这些方法中的任何一种都将消除您的 NullReferenceException
查看您使用 ArrayOfReceipt class 的方式,您可能需要考虑将 item
的类型更改为 List<Receipt>
。这样可以更轻松地更改您存储的收据 classes 的数量。
根据您尝试执行的操作,您可能还想在 ArrayOfReceipts 中创建一个 AddReceipt
方法并将您的 PropertyChanged
事件移至该方法。现在,PropertyChanged
事件只会在您的 ArrayOfReceipts
class 覆盖其数组 Receipts
.