0

I have been trying to figure out what is happening with my code. I wrote an application where the user is able to enter marks of the student through a GUI application. The first form shows options, the next form is to enter the student information (name, number, mark), and the last form is to display a summary of the student information (total number of students, highest mark, lowest mark, name of student with highest mark, list of students).

To store all the student entries, I had made a student class. I created a static Student array and placed it in my ProgramFunctions class file (which is all static methods).

When I try to run the form which displays the student summary, thats where it crashes - If I look at the Auto's tab, it tells me the value of student[a] is null (I made use of a for loop to go through each student object in the array). I did trace through the adding of students and it does show that I have added new entries to the Student array.

Exceptions would be thrown at my calculation methods (highestMark, lowestMark, average). Here is my code:

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

}

}

Okuhle
  • 842
  • 3
  • 14
  • 31
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Mar 20 '15 at 02:14
  • Please post the entire stacktrace – matt Mar 20 '15 at 02:14
  • It is impossible to tell whether any Student objects were actually created, populated and added to the array (that code is not in your post). Also not clear from your post what the null reference is actually referring to, but if it is referring to studentlist[a] (which starts at 0), then you have not put a Student object into that array. – Ric Gaudet Mar 20 '15 at 02:32
  • Please add `try` and `catch` blocks in your code. – Hui Zhao Mar 20 '15 at 02:34
  • Thank you so much for responding to my thread guys. The issue with my code was that I was using the Count () method of the Student Array. I used my counter variable and everything worked :) yes I could have used anything along the lines of ArrayList or LinkedList or List <>. – Okuhle Mar 21 '15 at 06:46

1 Answers1

0

It seems to me that if the student list isn't full accessing any index at counter or higher will result in a null object. I would suggest only looping up to counter:

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

All this begs the question why use a static array for dynamic data when List<T> is available and is made for dynamic data.

tinstaafl
  • 6,908
  • 2
  • 15
  • 22