抛出C#NullReferenceException:我的StudentList静态类数组为null
我一直在试图弄清楚我的代码到底发生了什么。我编写了一个应用程序,用户可以通过GUI应用程序输入学生的分数。第一个表单显示选项,下一个表单输入学生信息(姓名、编号、分数),最后一个表单显示学生信息摘要(学生总数、最高分数、最低分数、最高分数学生姓名、学生列表) 为了存储所有的学生条目,我创建了一个学生类。我创建了一个静态Student数组,并将其放在我的ProgramFunctions类文件中(这都是静态方法) 当我尝试运行显示学生摘要的表单时,它会崩溃——如果我查看Auto的选项卡,它会告诉我学生[a]的值为null(我使用for循环遍历数组中的每个学生对象)。我确实跟踪了添加学生的过程,它确实表明我已将新条目添加到学生数组中 异常将在我的计算方法中抛出(最高标记、最低标记、平均)。这是我的密码:抛出C#NullReferenceException:我的StudentList静态类数组为null,c#,nullreferenceexception,C#,Nullreferenceexception,我一直在试图弄清楚我的代码到底发生了什么。我编写了一个应用程序,用户可以通过GUI应用程序输入学生的分数。第一个表单显示选项,下一个表单输入学生信息(姓名、编号、分数),最后一个表单显示学生信息摘要(学生总数、最高分数、最低分数、最高分数学生姓名、学生列表) 为了存储所有的学生条目,我创建了一个学生类。我创建了一个静态Student数组,并将其放在我的ProgramFunctions类文件中(这都是静态方法) 当我尝试运行显示学生摘要的表单时,它会崩溃——如果我查看Auto的选项卡,它会告诉我学
class ProgramFunctions
{
private static Student[] studentList = new Student[25];
private static int counter = 0;
public static void addNewStudent(Student newStudent)
{
if (studentList.Count() == counter)
{
MessageBox.Show("The Student List is Full", "List is Full");
}
else
{
studentList[counter] = newStudent;
counter++;
}
}
public static void displayErrorMessage(String message, String title)
{
MessageBox.Show(message, title);
}
public static TextBox validateTextBox(int textboxNumber, TextBox thisTextBox)
{
if (textboxNumber.Equals(1)) //Student Name textbox
{
if (thisTextBox.Text.Length < 0 || thisTextBox.Text.Length > 100)
{
displayErrorMessage("The Student Name specified is out of allowed region (greater than 100 or less than 0 characters. Please fix this", "Student Name Error");
}
else
{
thisTextBox.Text = thisTextBox.Text.Trim();
return thisTextBox;
}
}
else if (textboxNumber.Equals(2)) //Student number text box (only 10 characters allowed)
{
if (thisTextBox.Text.Length < 0 || thisTextBox.Text.Length > 10)
{
displayErrorMessage("The student number you specified is greater than 10 characters or less than 0. Please fix this", "Student Number Error");
}
else
{
thisTextBox.Text = thisTextBox.Text.Trim();
return thisTextBox;
}
}
else
{
if (thisTextBox.Text.Length > 2 || thisTextBox.Text.Length < 0)
{
displayErrorMessage("Invalid Length for exam mark", "Invalid Exam Mark");
}
else
{
thisTextBox.Text = thisTextBox.Text.Trim();
return thisTextBox;
}
}
return null;
}
public static int getMaximumMarkPosition()
{
int highestMark = -999;
int positionFound = -1;
for (int a = 0; a < counter; a++)
{
if (studentList[a].getExamMark > highestMark)
{
highestMark = studentList[a].getExamMark; //This is where the error would occur
positionFound = a;
}
}
return positionFound;
}
public static int getHighestMark(int position)
{
return studentList[position].getExamMark;
}
public static int getLowestMark(int position)
{
return studentList[position].getExamMark;
}
public static string getHighestStudentMarkName(int position)
{
return studentList[position].getStudentName;
}
public static int getLowestMarkPosition()
{
int lowestMark = 999;
int positionFound = -1;
int studentMark = 0;
for (int a = 0; a < studentList.Count(); a++)
{
studentMark = studentList[a].getExamMark; //This is where the error would occur
if (studentMark < lowestMark)
{
lowestMark = studentMark;
positionFound = a;
}
}
return positionFound;
}
public static double calculateClassAverage()
{
double sum = 0;
double average = 0;
for (int a = 0; a < studentList.Count(); a++)
{
sum = sum + studentList[a].getExamMark;
}
average = sum / studentList.Count();
return average;
}
public static int getTotalNumberOfStudents()
{
return counter;
}
public static RichTextBox getTextBoxData(RichTextBox thisTextBox)
{
thisTextBox.Text = "STUDENT MARK DATA: \n\n";
for (int a = 1; a < studentList.Count(); a++)
{
Student currentStudent = returnStudentInformation(a);
thisTextBox.Text = thisTextBox.Text + "\n" + currentStudent.getStudentName + "\t\t" + currentStudent.getExamMark + "\t" + currentStudent.getStudentNumber;
}
return thisTextBox;
}
public static Student returnStudentInformation(int index)
{
return studentList[index];
}
}
类编程函数
{
私人静态学生[]学生列表=新学生[25];
专用静态整数计数器=0;
公共静态无效添加新闻学生(学生新闻学生)
{
if(studentList.Count()==计数器)
{
MessageBox.Show(“学生列表已满”、“列表已满”);
}
其他的
{
学生名单[计数器]=新学生;
计数器++;
}
}
公共静态void displayErrorMessage(字符串消息、字符串标题)
{
MessageBox.Show(消息、标题);
}
公共静态文本框validateTextBox(int-textboxNumber,TextBox-thisTextBox)
{
if(textboxNumber.Equals(1))//学生姓名textbox
{
如果(thisTextBox.Text.Length<0 | | thisTextBox.Text.Length>100)
{
displayErrorMessage(“指定的学生姓名超出了允许的区域(大于100或小于0个字符。请修复此错误”,“学生姓名错误”);
}
其他的
{
thisTextBox.Text=thisTextBox.Text.Trim();
返回此文本框;
}
}
else if(textboxNumber.Equals(2))//学生编号文本框(仅允许10个字符)
{
如果(thisTextBox.Text.Length<0 | | thisTextBox.Text.Length>10)
{
displayErrorMessage(“您指定的学号大于10个字符或小于0。请修复此问题”,“学号错误”);
}
其他的
{
thisTextBox.Text=thisTextBox.Text.Trim();
返回此文本框;
}
}
其他的
{
如果(thisTextBox.Text.Length>2 | | thisTextBox.Text.Length<0)
{
displayErrorMessage(“考试分数长度无效”、“考试分数无效”);
}
其他的
{
thisTextBox.Text=thisTextBox.Text.Trim();
返回此文本框;
}
}
返回null;
}
公共静态int getMaximumMarkPosition()
{
整数最高分数=-999;
int positionFound=-1;
对于(int a=0;ahighestMark)
{
highestMark=studentList[a].getExamMark;//这是发生错误的地方
位置=a;
}
}
找到返回位置;
}
公共静态int getHighestMark(int位置)
{
返回学生列表[位置].getExamMark;
}
公共静态整数getLowestMark(整数位置)
{
返回学生列表[位置].getExamMark;
}
公共静态字符串getHighestStudentMarkName(int位置)
{
返回studentList[position].getStudentName;
}
公共静态整型getLowestMarkPosition()
{
int lowestMark=999;
int positionFound=-1;
int studentMark=0;
对于(int a=0;a
}在我看来,如果学生列表未满,访问计数器或更高位置的任何索引将导致空对象。我建议只循环到计数器:
for (int a = 1; a < counter; a++)
{
Student currentStudent = returnStudentInformation(a);
thisTextBox.Text = thisTextBox.Text + "\n" + currentStudent.getStudentName + "\t\t" + currentStudent.getExamMark + "\t" + currentStudent.getStudentNumber;
}
for(int a=1;a
所有这些都引出了一个问题:为什么