空引用异常将对象添加到列表时

时间:2013-06-13 20:47:50

标签: c# c#-4.0

当我尝试将对象添加到对象内的列表时,即使对象的所有属性都包含数据,我仍然会收到NullReferenceException。 类 -

public class OrderInfo
    {
        public virtual string OrderNum { get; set; }
        public virtual string TrackingNum { get; set; }
        public virtual DateTime Shipdate { get; set; }
        public virtual string Cost { get; set; }
        public virtual string ShipMethod { get; set; }
        public virtual string ShipService { get; set; }
        public virtual string Country { get; set; }
        public virtual decimal Weight { get; set; }
        public virtual List<OrderItemInfo> OrderiTems { get; set; }

        public void AddShipmentItem(OrderItemInfo oi)
        {
            this.OrderiTems.Add(oi); // NULL Reference HERE
        }

    }

    public class OrderItemInfo
    {
        public virtual string OrderItemCode { get; set; }
        public virtual decimal? Quantity { get; set; }
        public virtual decimal? Cost { get; set; }
        public virtual decimal? Weight { get; set; }
        public virtual string Store { get; set; }
    }

然后,如果存在任何可以为空的数据,我会捕获代码。

private static OrderInfo GetOrderInfo(DataRow dr)
    {
        SqlConnection ShipworksConnectionString = 
        SqlCommand ShipworksCmd = new SqlCommand("SELECT OrderItem.Code,  

        InternationalShipmentCostAnalysisApp.OrderInfo ip = new      InternationalShipmentCostAnalysisApp.OrderInfo
            {
                OrderNum = (dr[0] is DBNull) ? String.Empty : dr[0].ToString(),
                TrackingNum = (dr[1] is DBNull) ? String.Empty : dr[1].ToString(),
                Shipdate = (dr[2] is DBNull) ? DateTime.MinValue :  Convert.ToDateTime(dr[2]),
                Cost = (dr[3] is DBNull) ? String.Empty : dr[3].ToString(),
                ShipMethod = (dr[4] is DBNull) ? String.Empty : dr[4].ToString(),
                ShipService = (dr[5] is DBNull) ? String.Empty : dr[5].ToString(),
                Country = (dr[6] is DBNull) ? String.Empty : dr[6].ToString(),
                Weight = (dr[7] is DBNull) ? 0 : Convert.ToDecimal(dr[7])

            };

        ShipworksConnectionString.Open();

        SqlDataReader rdr = ShipworksCmd.ExecuteReader();
        DataTable dt = new DataTable();

        dt.Load(rdr);
        rdr.Close();
        ShipworksConnectionString.Close();

        foreach (DataRow item in dt.Rows)
        {
            if (item != null)
            {
                InternationalShipmentCostAnalysisApp.OrderItemInfo i = new InternationalShipmentCostAnalysisApp.OrderItemInfo
                {
                    OrderItemCode = (item[0] is DBNull) ? String.Empty : item[0].ToString(),
                    Quantity = (item[1] is DBNull) ? 0 : Convert.ToDecimal(item[1]),
                    Cost = (item[2] is DBNull) ? 0 : Convert.ToDecimal(item[2]),
                    Weight = (item[3] is DBNull) ? 0 : Convert.ToDecimal(item[3]),
                    Store = (item[4] is DBNull) ? String.Empty : item[4].ToString()

                };

                ip.AddShipmentItem(i);
            }

        }


        return ip;
    }

4 个答案:

答案 0 :(得分:18)

您似乎永远不会设置您的属性...您已将其定义为List<OrderItemInfo> 类型但您从未将其初始化为该类型的实例 。尝试在构造函数中初始化它:

public class OrderInfo
{
    public OrderInfo
    {
        OrderiTems = new List<OrderItemInfo>();
    }
}

答案 1 :(得分:5)

OrderiTemsnull

public virtual List<OrderItemInfo> OrderiTems { get; set; }

尝试在构造函数中实现set / get或将空列表存储到OrderiTems

答案 2 :(得分:2)

您需要在开始使用之前实例化列表。这样做的好地方是在类构造函数中。假设您使用默认构造函数:

public OrderInfo()
{
    this.OrderiTems = new List<OrderItemInfo>();
}

答案 3 :(得分:0)

DbNull与Is NULL不同!

您需要检查“IS NULL”。这就是你得到空参考例外的原因。

编辑:,因为ebyrob说得对,以下行抛出异常:

this.OrderiTems.Add(oi); // NULL Reference HERE