I have a assignment from school and I’m almost finished, only one thing left. I get a NullReferenceException every time I start the program. Everything works as it’s supposed to except for the listview where the exception is thrown.
This is from the 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);
}
}
}
It’s this line that throws the exception:
seats[i].Split('|');
Here’s the method GetSeatInfoString from the SeatManager class:
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;
}
I know what a NullReferenceException is but I can’t find why I get it. The array seats should be filled, if that’s the problem what’s wrong in the method GetSeatInfoString?
This line:
The order is unclear, but there are two assignments to
seatshere – The first via theout, the second with allnulls (a new string-array). Try instead:Which only assigned via the “out”.