Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null Reference Exception When adding object to list [closed]

Tags:

c#

c#-4.0

I keep getting an NullReferenceException when I try to add an object to a list inside an object, even when all properties of the object contains data. Classes--

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; }
    }

Then I have code that catches if any nullable data is there.

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;
    }
like image 963
zerodoc Avatar asked Jun 13 '13 20:06

zerodoc


1 Answers

It seems you never SET your property... You have it defined as a List<OrderItemInfo> type but you never initialize it to an instance of that type. Try initializing it in a constructor:

public class OrderInfo
{
    public OrderInfo
    {
        OrderiTems = new List<OrderItemInfo>();
    }
}
like image 76
Haney Avatar answered Oct 21 '22 01:10

Haney