正在爲一個類的程序工作,並且已完成約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是我想要顯示座位列表的位置。
我真誠希望這不是一個生產代碼。將此添加到'公共Flight(){this.mSeatChart = new string [1]; }' – zaitsman
你能提供堆棧跟蹤嗎? –
對象參考不WindowsFOrmasAPplication3.Reservations.DisplayNewFlightCHart()在 d設置爲一個對象 System.NullReferenceException 的一個實例:\ Users \用戶森\桌面\ C# 項目\ program4 \ windowsformsappliocation3 \ Form1.cs中:行76 – Ozmethod