大家好我正在尝试将另一个类的值添加到MainForm上的ListBox但是我收到错误NullReferenceException未处理。这是我的代码:
public partial class MainForm : Form
{
Seats currentSeat = new Seats();
public MainForm()
{
InitializeComponent(); // VS' s code
//1. Prepare the form before it is shown to the user.
InitializeGUI();
}
private void InitializeGUI()
{
currentSeat.Seat();
}
**class Seats**
{
private string customerName = "Me ";
MainForm mainForm;
public void Seat()
{
SetDefaultValues();
}
private void SetDefaultValues(){
for (int seat = 1; seat <= 60; seat++)
{
mainForm.listBoxReservations.Items.Add(string.Format("{0} {1} ", seat,costumerName)); // **HERE IS THE ERROR NullReferenceException was unhandled**
}
}
}
任何建议
答案 0 :(得分:-1)
您尚未创建MainForm类 AND 拼写错误的customerName:
private void SetDefaultValues(){
for (int seat = 1; seat <= 60; seat++)
{
mainForm.listBoxReservations.Items.Add(string.Format("{0} {1} ", seat,costumerName)); // **HERE IS THE ERROR NullReferenceException was unhandled**
}
您的实例变量说明了这一点:
private string customerName = "Me ";
但是你的代码引用了这个:
costumerName