无法弄清楚为什么Object引用为null

时间:2015-06-06 22:54:03

标签: c#

在课堂上学习,大约95%完成,但遇到了障碍。我有一个飞行课程,里面有飞行信息,还有座位表。使用Windows窗体列表框从我通过读取文本文件创建的航班对象中进行选择。我可以从类对象获取每个属性的值,除了一个,SeatChart。

以下是主程序中的相关代码:

    private void lstFlights_SelectedIndexChanged(object sender, EventArgs e)
    {
        curFlight = (Flight)lstFlights.SelectedItem;
        DisplayNewFlightChart();
    }

    private void DisplayNewFlightChart()
    {

        int seats = curFlight.Rows * curFlight.Seats;
        lstSeatingChart.Items.Clear();
        string[] seatChart = curFlight.SeatChart;
        for (int x = 0; x <= seats; x++)
        {
            lstSeatingChart.Items.Add("Seat " + (x + 1) + " " + seatChart[x]);
        }
    }

以下是该类的代码:

class Flight
{
    private string mPlane;
    private string mDepartureTime;
    private string mDestination;
    private int mRows;
    private int mSeats;
    private string[] mSeatChart;

    public Flight()
    {
    }

    // Create the overloaded Constructor
    public Flight(string planeType, string departureTime,
                  string destination, int numRows,
                  int numSeatsPerRow)
    {
        this.Plane = planeType;
        this.DepartureTime = departureTime;
        this.Destination = destination;
        this.Rows = numRows;
        this.Seats = numSeatsPerRow;
        this.SeatChart = mSeatChart;


        mSeatChart = new string[Rows * Seats];

        for (int seat = 0; seat <= mSeatChart.GetUpperBound(0); seat++)
        {
            mSeatChart[seat] = "Open";
        }



    }

    public string Plane
    {
        get { return mPlane; }
        set { mPlane = value; }
    }

    public string DepartureTime
    {
        get { return mDepartureTime; }
        set { mDepartureTime = value; }
    }

    public string Destination
    {
        get { return mDestination; }
        set { mDestination = value; }
    }

    public int Rows
    {
        get { return mRows; }
        set { mRows = value; }
    }

    public int Seats
    {
        get { return mSeats; }
        set { mSeats = value; }
    }

    public string[] SeatChart
    {
        get { return mSeatChart; }
        set { mSeatChart = value; }
    }

    public void MakeReservation(string passName, int seat)
    {
        bool seatTaken = false;
        if (mSeatChart[seat] != "Open") seatTaken = true;

        if (passName != "" && seatTaken == false)
        {
            mSeatChart[seat] = passName;
        }
        else
        {
            MessageBox.Show("Please Enter a Passenger Name, in an unreserved seat");
        }

    }

它告诉我curFlight.SeatChart为空,即使我可以从curFlight中拉出.Rows和.Seats就好了。我不知道为什么.SeatChart搞砸了。 lstFlights是从文本文件中提取的航班对象列表,而lstSeatingChart是我想要显示座位列表的位置。

2 个答案:

答案 0 :(得分:3)

您正在将SeatChart设置为mSeatChart,此时为空。所以没有为this.SeatChart。

引用一个对象

之后,您初始化mSeatChart并填充它。

初始化mSeatChart后,您应该移动设置this.SeatChart。

mSeatChart = new string[Rows * Seats];
this.SeatChart = mSeatChart;

编辑: 另外,SeatChart是属性,mSeatChart是成员变量。 SeatChart将用于公开mSeatChart,因此使用mSeatChart设置SeatChart真的很奇怪。很奇怪,我甚至认为你没有这样做。 因此,在您的情况下,请在构造函数中保留以下内容:

this.SeatChart = mSeatChart;

我认为问题的实际原因是代码中的其他位置,您可以在其中启动Flight并填写列表。如果我理解正确,你会在for循环的连接中得到一个空引用错误吗?

    string[] seatChart = curFlight.SeatChart;
    for (int x = 0; x <= seats; x++)
    {
        lstSeatingChart.Items.Add("Seat " + (x + 1) + " " + seatChart[x]);
    }

检查每个Flight对象的启动位置。我打赌你使用的是空构造函数:new Flight() 删除空构造函数,因为您显然不会期望空值。如果你真的需要空构造函数,那么要么按预期启动所有成员变量,要么在你想要使用它们的地方执行空值检查。

一旦找到原因,请确保将for循环更改为

    for (int x = 0; x < seats; x++)

因为您正在检查座位数并执行从零开始的循环。如果x =座位,你将执行循环座位+ 1次(行*座位+ 1)。

答案 1 :(得分:0)

如果您的代码依赖于永远不为null的特定属性,则需要确保它在所有构造函数中初始化。

根据你班级的逻辑,我建议你不要有一个参数少的构造函数。拥有一个没有已知席位数的航班(至少在你的实施中)是没有意义的。

也是一些风格的东西。

您不需要声明私有实例变量。只需使用

SO_REUSEADDR

宣布&#34;开放&#34;作为类常量并使用该常量而不是硬编码的字符串值。