C#NullReferenceException抛出:我的StudentList静态类数组为null

时间:2015-03-20 02:11:24

标签: c# nullreferenceexception

我一直试图弄清楚我的代码发生了什么。我写了一个应用程序,用户可以通过GUI应用程序输入学生的标记。第一个表单显示选项,下一个表单是输入学生信息(姓名,号码,标记),最后一个表单是显示学生信息摘要(学生总数,最高分,最低分,姓名)最高分的学生,学生名单)。

为了存储所有学生的条目,我做了一个学生班。我创建了一个静态Student数组并将其放在我的ProgramFunctions类文件中(这是所有静态方法)。

当我尝试运行显示学生摘要的表单时,那就是它崩溃的地方 - 如果我查看Auto的选项卡,它会告诉我student [a]的值为null(我使用for循环来遍历数组中的每个学生对象)。我确实通过添加学生进行追踪,它确实表明我已经在学生数组中添加了新条目。

我的计算方法(highestMark,lowestMark,average)会引发例外情况。这是我的代码:

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];
    }

}

}

1 个答案:

答案 0 :(得分:0)

在我看来,如果学生列表没有完全访问计数器或更高的任何索引将导致空对象。我建议只循环计数器:

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;
}

所有这些都引出了一个问题,即当List<T>可用时,为动态数据使用静态数组,并为动态数据做出这一点。