当我制作seats[] seatArray
并尝试运行它时,我收到错误
NullReferenceException未处理。你调用的对象是空的。
我认为最后是我的printinfo功能。
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Seats s = new Seats();
//Seats[] seatArray = new Seats[14]; //HEREEE
public Form1()
{
InitializeComponent();
listBox2.Items.Add("ROW# A B C D E F");
listBox2.Items.Add(" 1 " + s.PrintInfo);
// listBox2.Items.Add( seatArray[0].PrintInfo); //HEREEE
listBox1.Items.Add("Seats Filled:");
listBox1.Items.Add("Windows Available:");
listBox1.Items.Add("Regular Meals:");
listBox1.Items.Add("LowCal Meals:");
listBox1.Items.Add("Veget Meals:");
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
string test = listBox2.SelectedItem.ToString(); /
//string[] div = Regex.Split(test, " ");
//textBox2.Text = div[0];
textBox2.Text = test; //
}
}
public class Seats //
{
public char A;
public char B;
public char C;
public char D;
public char E;
public char F;
public Seats()
{
A = '.';
B = '.';
C = '.';
D = '.';
E = '.';
F = '.';
}
public char Aa
{
set { A = value; }
get { return A; }
}
public char Bb
{
set { A = value; }
get { return A; }
}
public char Cc
{
set { C = value; }
get { return C; }
}
public char Dd
{
set { D = value; }
get { return D; }
}
public char Ee
{
set { E = value; }
get { return E; }
}
public char Ff
{
set { F = value; }
get { return F; }
}
public string PrintInfo //
{
get { return this.A + " " + this.B + " " + this.C + " " + this.D + " " + this.E + " " + this.F; }
}
}
}
答案 0 :(得分:2)
创建数组时,它将填充给定类型的默认值。对于参考类型,默认值为null
。您需要先将数据添加到数组中,然后才能使用它们:
Seats[] seatArray = new Seats[14];
seatArray[0] = new Seat(); // or something like this
public Form1()
{
InitializeComponent();
listBox2.Items.Add("ROW# A B C D E F");
listBox2.Items.Add(" 1 " + s.PrintInfo);
listBox2.Items.Add( seatArray[0].PrintInfo); //HEREEE
如果您想创建一个包含14个未初始化席位的阵列,您可以使用:
Seats[] seatArray = Enumerable.Repeat(new Seat(),14).ToArray();