我試圖早些時候獲得一些幫助,但我不認爲我提供了足夠的信息,儘管我很欣賞所有的建議。調用對象數組時調用了空引用異常
目標僅僅是將對象房間的新實例添加到數組並打印到列表框。當用戶試圖輸入一個已經存在的房間名時,它應該只顯示已經在該房間中的房間的規格。
我不斷收到空引用異常。
這裏是我的代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Room_Class
{
public partial class Form1 : Form
{
Room[] roomArray = new Room[20];
int count = 0;
public Form1()
{
InitializeComponent();
}
private void btnAddRm_Click(object sender, EventArgs e)
{
double length, width, height;
if (VerifyRoomName() == true)
{
if (txtRmLen.Text == "" || txtRmWid.Text == "" || txtRmHt.Text == "")
{
for (int i = 0; i < roomArray.Length; i++)
{
if (txtRmName.Text == roomArray[i].getRoomName())
{
txtRmName.Text = roomArray[i].getRoomName();
txtRmLen.Text = roomArray[i].getLength().ToString();
txtRmHt.Text = roomArray[i].getCeilingHeight().ToString();
txtRmWid.Text = roomArray[i].getWidth().ToString();
}
else
{
roomArray[count] = new Room(roomName);
count++;
}
}
}
else
{
try
{
length = double.Parse(txtRmLen.Text);
width = double.Parse(txtRmWid.Text);
height = double.Parse(txtRmHt.Text);
for (int i = 0; i < roomArray.Length; i++)
{
if (txtRmName.Text == roomArray[i].getRoomName())
{
txtRmName.Text = roomArray[i].getRoomName();
txtRmLen.Text = roomArray[i].getLength().ToString();
txtRmHt.Text = roomArray[i].getCeilingHeight().ToString();
txtRmWid.Text = roomArray[i].getWidth().ToString();
}
else
{
roomArray[count] = new Room(roomName, length, width, height);
count++;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\nPlease Enter Valid Values", "Error!");
}
}
PrintList();
}
}
private void PrintList()
{
double paintTotal = 0, feetTotal = 0;
string RoomName;
lstRoomList.Items.Clear();
for (int i = 0; i < count; i++)
{
RoomName = roomArray[i].getRoomName() + "\t" + roomArray[i].SquareFeet().ToString("n1") + "\t" + roomArray[i].GallonsPaint().ToString("n1");
lstRoomList.Items.Add(RoomName);
paintTotal += roomArray[i].GallonsPaint();
feetTotal += roomArray[i].SquareFeet();
lblTtlGallons.Text = paintTotal.ToString("n1");
lblTtlSqFt.Text = feetTotal.ToString("n1");
}
}
private bool VerifyRoomName()
{
if (roomName == "")
{
MessageBox.Show("Please Enter a Room Name", "Error!");
return false;
}
else
return true;
}
}
}
什麼是ROOMNAME – 2013-02-27 02:07:40
哪裏是例外? – John3136 2013-02-27 02:08:02
歡迎來到Stack Overflow!幾乎所有的'NullReferenceException'都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 – 2013-02-27 02:08:20