为什么我会得到NullReferenceException?

时间:2011-10-15 09:24:26

标签: c# nullreferenceexception

我有一份来自学校的作业,我差不多完成了,只留下了一件事。每次启动程序时都会出现NullReferenceException。除了抛出异常的列表视图之外,一切都按预期工作。

这是来自MainForm:

private void UpdateListView()
{
    lstReservations.Clear();

    string[] seats = new string[m_seatMngr.GetSeatInfoStrings((SeatManager.DisplayOption)cmboBoxListAlternitives.SelectedIndex, out seats)];

    if (seats != null && seats.Length > 0)
    {
        string[] split = new string[4];

        for (int i = 0; i < seats.Length; i++)
        {
            split = seats[i].Split('|');

            ListViewItem newItem = new ListViewItem(split[0]);
            newItem.SubItems.Add(split[1]);
            newItem.SubItems.Add(split[2]);
            newItem.SubItems.Add(split[3]);
            //Lägger till newItem till lstReservations
            lstReservations.Items.Add(newItem);
        }
    }
}

这条线引发了异常:

seats[i].Split('|');

以下是SeatManager类的GetSeatInfoString方法:

public int GetSeatInfoStrings(DisplayOption choice, out string[] strSeatInfoStrings)
{
    strSeatInfoStrings = null;
    int count = GetNumOfSeats(choice);

    if (count <= 0)
    {
        return 0;
    }

    strSeatInfoStrings = new string[count];

    int i = 0; //counter for return array
    //Is the element corresponding with the index empty
    for (int index = 0; index < m_totNumOfSeats; index++)
    {
        switch (choice)
        {
            case DisplayOption.AllSeats:
                strSeatInfoStrings[index] = GetSeatInfoAt(index);
                i++;
                break;
            case DisplayOption.ReservedSeats:
                if (m_nameList[index] != null)
                {
                    strSeatInfoStrings[i] = GetSeatInfoAt(index);

                    i++;
                }
                break;
            case DisplayOption.VacantSeats:
                if (m_nameList[index] == null)
                {
                    strSeatInfoStrings[i] = GetSeatInfoAt(index);

                    i++;
                }
                break;
            default:
                break;
        }
    }

    return i;
}

我知道NullReferenceException是什么,但我找不到为什么会得到它。如果问题是方法GetSeatInfoString有什么问题,那么阵列座位应该被填充?

2 个答案:

答案 0 :(得分:3)

这一行:

string[] seats = new string[m_seatMngr.GetSeatInfoStrings((SeatManager.DisplayOption)
    cmboBoxListAlternitives.SelectedIndex, out seats)];

订单不清楚,但此处seats两个分配 - 第一个来自out,第二个来自所有null s(a新的字符串数组)。请尝试改为:

string[] seats;
m_seatMngr.GetSeatInfoStrings((SeatManager.DisplayOption)
    cmboBoxListAlternitives.SelectedIndex, out seats);

仅通过“out”分配。

答案 1 :(得分:1)

您应该学会使用调试器来帮助追踪问题。如果您单步执行代码,则会看到seats仅包含null值。

问题在于这一行:

string[] seats = new string[m_seatMngr.GetSeatInfoStrings((SeatManager.DisplayOption)cmboBoxListAlternitives.SelectedIndex, out seats)];

在分配发生之前,必须评估=的右侧。因此,即使GetSeatInfoStrings分配给seats,它也会被分配覆盖。

最后你要做的就是:

string[] seats = new string[some_number];

该数组中的每个元素都将初始化为null

从某种意义上说,你在代码中做得太多了。您正在分配seats数组两次。您所需要做的就是:

string[] seats;
m_seatMngr.GetSeatInfoStrings((SeatManager.DisplayOption)cmboBoxListAlternitives.SelectedIndex, out seats);
相关问题