c# - 未处理C#NullReferenceException-未将对象引用设置为对象的实例

标签 c# exception reference null unhandled

我正在尝试使用C#中的Windows窗体创建应用程序,但是我不断收到错误消息:


  未处理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;
    }
}

最佳答案

变量carBootSaleList尚未初始化,因此为null

尝试更换

CarBootSaleList carBootSaleList;




CarBootSaleList carBootSaleList = new CarBootSaleList();

关于c# - 未处理C#NullReferenceException-未将对象引用设置为对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15112456/

相关文章:

c# - 我可以用字符串变量(或其他东西)命名数组的每个元素吗?

c# - jQuery 表单提交函数中的 AJAX 调用

java - java中十六进制字符串到整数解析异常处理

c++ - 引用抽象类的类的 copy-and-swap 习语

Java:全局更新对象引用

python - 在 python 字典中赋值(复制与引用)

c# - mscorlib.dll 中发生奇怪的错误 'System.ExecutionEngineException'

c# - WCF 与 Java 兼容吗?

c++ - 按值或引用抛出异常

exception - 更改企业库异常处理格式化程序中的时间戳值