2013-02-27 46 views
1

我试图创建使用Windows窗体在C#应用程序的实例,但是,我不断收到错误提示:C#的NullReferenceException是未处理 - 对象引用不设置到对象

的NullReferenceException是未处理 - 对象引用不设置到对象的实例

它指向这对这个代码行:

carBootSaleList.AddCarBootSale(newCarBootSale); 

该方法我有形式接口:

CarBootSaleList carBootSaleList; 

    public void AddCarBootSale() 
    { 
     AddNewCarBootSale addForm = new AddNewCarBootSale(); 
     if (addForm.ShowDialog() == DialogResult.OK) 
     { 
      CarBootSale newCarBootSale = addForm.GetCarBootSaleData(); 

      carBootSaleList.AddCarBootSale(newCarBootSale); 

      txtCarBootSale.Clear(); 
      txtCarBootSale.Text = newCarBootSale.Display(); 
     } 
    } 

的CarBootSaleList类:

public class CarBootSaleList : IDisplay 
{ 

    private List<CarBootSale> carbootsales; 

    public CarBootSaleList() 
    { 
     carbootsales = new List<CarBootSale>(); 
    } 

    public bool AddCarBootSale(CarBootSale carbootsale) 
    { 
     bool success = true; 
     foreach (CarBootSale cbs in carbootsales) 
     { 
      if (cbs.ID == carbootsale.ID) 
      { 
       success = false; 
      } 
     } 
     if (success) 
     { 
      carbootsales.Add(carbootsale); 
     } 
     return success; 
    } 

    public int GetListSize() 
    { 
     return carbootsales.Count(); 
    } 

    //public CarBootSale FindCarBootSale(string cbsID) 
    //{ 
    // CarBootSale carbootsale = null; 
    // foreach (CarBootSale cbs in carbootsale) 
    // { 
    //  if (cbs.ID == cbsID) 
    //  { 
    //   carbootsale = cbs; 
    //  } 
    // } 
    // return carbootsale; 
    //} 

    public List<CarBootSale> ReturnList() 
    { 
     return carbootsales; 
    } 

    public string Display() 
    { 
     string msg = ""; 

     foreach (CarBootSale cbs in carbootsales) 
     { 
      msg += String.Format("{0} {1}", cbs.ID, cbs.Location, cbs.Date); 
      msg += Environment.NewLine; 
     } 
     return msg; 
    } 
} 

的CarBootSale类:

public class CarBootSale : IDisplay 
{ 

    public string ID { get; set; } 
    public string Date { get; set; } 
    public string Location { get; set; } 
    public double PitchCost { get; set; } 
    public int Capacity { get; set; } 
    public string Charity { get; set; } 
    public string CharityName { get; set; } 
    public string Catering { get; set; } 

    public CarBootSale(string id, string date, string location, double pitchcost, int capacity, string charity, string charityname, string catering) 
    { 
     ID = id; 
     Date = date; 
     Location = location; 
     PitchCost = pitchcost; 
     Capacity = capacity; 
     Charity = charity; 
     CharityName = charityname; 
     Catering = catering; 
    } 

    public CarBootSale() { } 

    public override string ToString() 
    { 
     return String.Format("{0} {1}", ID, Date, Location); 
    } 

    public string Display() 
    { 
     string msg; 
     string CR = Environment.NewLine; 

     msg = String.Format("ID: {0} {1}", ID, CR); 
     msg += String.Format(" Date: {0} {1}", Date, CR); 
     msg += String.Format(" Location: {0} {1}", Location, CR); 
     msg += String.Format(" Pitch Cost: {0} {1}", PitchCost, CR); 
     msg += String.Format(" Capacity: {0} {1}", Capacity, CR); 
     msg += String.Format(" Charity: {0} {1}", Charity, CR); 
     msg += String.Format(" CharityName: {0} {1}", CharityName, CR); 
     msg += String.Format(" Catering: {0} {1}", Catering, CR); 

     return msg; 
    } 
} 
+3

您是否实例化了carbootSaleList? – Charleh 2013-02-27 12:45:08

回答

5

可变carBootSaleList未初始化,因此它是null

尝试

CarBootSaleList carBootSaleList = new CarBootSaleList(); 
5

你有没有在你的代码初始化carBootSaleList任何地方。

您可以在方法中或声明中实例化它。

CarBootSaleList carBootSaleList = new CarBootSaleList(); 
0

carBootSaleList为空。

某处,类型:

carBootSaleList = new CarBootSaleList(); 
1

您还没有初始化carBootSaleList更换

CarBootSaleList carBootSaleList; 

CarBootSaleList carBootSaleList = new carBootSaleList(); 
相关问题