将信息添加到类中的数组

时间:2014-12-08 11:14:56

标签: c#

我已经使用xsd将一些xml转换为类,现在我遇到一个问题,即将一些数据放入类中存在的数组中以添加项列表。我正在尝试将信息导入OrderItemsItem。

有点难过,任何帮助表示感谢(如果我能逃脱它,不想真正热衷于对类进行任何更改,转换为List<>等)。

这是添加信息的代码:

OrderItemsItem orderItemsItem = new OrderItemsItem();
orderItemsItem.CostCentre = "sfsdf";
orderItemsItem.DeliveryDate = "2014-01-05";
orderItemsItem.Fund = "G";
orderItemsItem.ExternalLineRef = "1";
orderItemsItem.ItemName = "dfss";
orderItemsItem.LineNo = "1";
orderItemsItem.ProdId = "dfsf";
orderItemsItem.Project = "";
orderItemsItem.QuantityOrdered = "2";
orderItemsItem.UnitCost = "10";

Order order = new Order();
OrderItemsItem [] items = {orderItemsItem};
  order.Items.Item = items;

我收到的错误正在发生 - > order.Items.Item = items;:

  

System.NullReferenceException:未将对象引用设置为实例   一个对象。

这是班级:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18444
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XXXXXXXXX", IsNullable=false)]
public partial class Order {

    private string referenceField;

    private string notesField;

    private string orderDateField;

    private string statusField;

    private OrderItems itemsField;

    private OrderBuyerDetails buyerDetailsField;

    /// <remarks/>
    public string Reference {
        get {
            return this.referenceField;
        }
        set {
            this.referenceField = value;
        }
    }

    /// <remarks/>
    public string Notes {
        get {
            return this.notesField;
        }
        set {
            this.notesField = value;
        }
    }

    /// <remarks/>
    public string OrderDate {
        get {
            return this.orderDateField;
        }
        set {
            this.orderDateField = value;
        }
    }

    /// <remarks/>
    public string Status {
        get {
            return this.statusField;
        }
        set {
            this.statusField = value;
        }
    }

    /// <remarks/>
    public OrderItems Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }

    /// <remarks/>
    public OrderBuyerDetails BuyerDetails {
        get {
            return this.buyerDetailsField;
        }
        set {
            this.buyerDetailsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderItems {

    private OrderItemsItem[] itemField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Item")]
    public OrderItemsItem[] Item {
        get {
            return this.itemField;
        }
        set {
            this.itemField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderItemsItem {

    private string lineNoField;

    private string externalLineRefField;

    private string prodIdField;

    private string itemNameField;

    private string quantityOrderedField;

    private string unitCostField;

    private string deliveryDateField;

    private string costCentreField;

    private string projectField;

    private string fundField;

    /// <remarks/>
    public string LineNo {
        get {
            return this.lineNoField;
        }
        set {
            this.lineNoField = value;
        }
    }

    /// <remarks/>
    public string ExternalLineRef {
        get {
            return this.externalLineRefField;
        }
        set {
            this.externalLineRefField = value;
        }
    }

    /// <remarks/>
    public string ProdId {
        get {
            return this.prodIdField;
        }
        set {
            this.prodIdField = value;
        }
    }

    /// <remarks/>
    public string ItemName {
        get {
            return this.itemNameField;
        }
        set {
            this.itemNameField = value;
        }
    }

    /// <remarks/>
    public string QuantityOrdered {
        get {
            return this.quantityOrderedField;
        }
        set {
            this.quantityOrderedField = value;
        }
    }

    /// <remarks/>
    public string UnitCost {
        get {
            return this.unitCostField;
        }
        set {
            this.unitCostField = value;
        }
    }

    /// <remarks/>
    public string DeliveryDate {
        get {
            return this.deliveryDateField;
        }
        set {
            this.deliveryDateField = value;
        }
    }

    /// <remarks/>
    public string CostCentre {
        get {
            return this.costCentreField;
        }
        set {
            this.costCentreField = value;
        }
    }

    /// <remarks/>
    public string Project {
        get {
            return this.projectField;
        }
        set {
            this.projectField = value;
        }
    }

    /// <remarks/>
    public string Fund {
        get {
            return this.fundField;
        }
        set {
            this.fundField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderBuyerDetails {

    private string nameField;

    private string emailField;

    private OrderBuyerDetailsBillingAddress billingAddressField;

    /// <remarks/>
    public string Name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    /// <remarks/>
    public string Email {
        get {
            return this.emailField;
        }
        set {
            this.emailField = value;
        }
    }

    /// <remarks/>
    public OrderBuyerDetailsBillingAddress BillingAddress {
        get {
            return this.billingAddressField;
        }
        set {
            this.billingAddressField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderBuyerDetailsBillingAddress {

    private string nameField;

    private string address1Field;

    private string address2Field;

    private string address3Field;

    private string placeField;

    private string countyField;

    private string postCodeField;

    private string countryField;

    private string emailField;

    /// <remarks/>
    public string Name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    /// <remarks/>
    public string Address1 {
        get {
            return this.address1Field;
        }
        set {
            this.address1Field = value;
        }
    }

    /// <remarks/>
    public string Address2 {
        get {
            return this.address2Field;
        }
        set {
            this.address2Field = value;
        }
    }

    /// <remarks/>
    public string Address3 {
        get {
            return this.address3Field;
        }
        set {
            this.address3Field = value;
        }
    }

    /// <remarks/>
    public string Place {
        get {
            return this.placeField;
        }
        set {
            this.placeField = value;
        }
    }

    /// <remarks/>
    public string County {
        get {
            return this.countyField;
        }
        set {
            this.countyField = value;
        }
    }

    /// <remarks/>
    public string PostCode {
        get {
            return this.postCodeField;
        }
        set {
            this.postCodeField = value;
        }
    }

    /// <remarks/>
    public string Country {
        get {
            return this.countryField;
        }
        set {
            this.countryField = value;
        }
    }

    /// <remarks/>
    public string Email {
        get {
            return this.emailField;
        }
        set {
            this.emailField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XXXXXXXXX", IsNullable=false)]
public partial class Post_Printondemand_Create_Full_Order_DataSet {

    private Order[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Order")]
    public Order[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

谢谢

2 个答案:

答案 0 :(得分:1)

 order.Items.Item = items

错误必须是订单中的某些订单的Items集合为null(因此无法访问其&#34; Item&#34;属性)。

答案 1 :(得分:0)

我的怀疑是在你的示例order.Items为null - 你需要附加一个调试器并自己检查变量以确定你的null ref的根本原因。在使用数组之前,请确保该数组不为空。

在您的示例中,您将覆盖数组的元素。这将替换现有元素(如果存在)或(我相信)抛出IndexOutOfBoundsException,如果它没有。

数组通常用于固定长度的数据,您不会经常附加/删除数据。列表是一个更好的集合,因为它们具有Add()和Remove()函数,特别是这个原因。

可以&#34;追加&#34;到C#数组

order.Items = Order.Items.Union(new Item[]{myNewItem}));

然而,它不是很好并且通过合并旧的阵列来创建一个新阵列!我的建议......让收藏成为一个清单!