C#NullReferenceException未处理 - 对象引用未设置为对象的实例 [英] C# NullReferenceException was unhandled - Object reference not set to an instance of an object

查看:264
本文介绍了C#NullReferenceException未处理 - 对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


NullReferenceException未被处理过,我试图在C#中使用Windows Forms创建一个应用程序, - 对象引用未设置为对象的实例


它指向具有以下代码的行:

  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。日期);
msg + = Environment.NewLine;
}
return msg;
}
}

CarBootSale类:

  public class CarBootSale:IDisplay 
{

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

public CarBootSale(string id,string date,string location,double pitchcost,int capacity,string charity,string charityname,string catering)
{
ID = id;
Date = date;
位置=位置;
PitchCost = pitchcost;
容量=容量;
慈善=慈善;
CharityName = charityname;
餐饮=餐饮;
}

public CarBootSale(){}

public override string ToString()
{
return String.Format({0 } {1},ID,日期,位置);
}

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(容量:{0} {1},容量,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(); 


I'm trying to create a application using Windows Forms in C#, however I keep getting an error stating:

NullReferenceException was unhandled - Object reference not set to an instance of an object

It's pointing to the line which has this code on:

carBootSaleList.AddCarBootSale(newCarBootSale);

The method I have on the forms interface:

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

The CarBootSaleList class:

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

The CarBootSale class:

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

解决方案

The variable carBootSaleList wasn't initialized, and it is therefore null.

Try replacing

CarBootSaleList carBootSaleList;

with

CarBootSaleList carBootSaleList = new CarBootSaleList();

这篇关于C#NullReferenceException未处理 - 对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆