0

I am getting this error and I have absolutely no idea how to get rid of it. I allocated the array but cant seem to get it initialized correctly. I allocated the array on line 13 NullReference error. I read about it but in this case I have no idea what I'm doing wrong.

I have the following code.

namespace A4
{
    public partial class frmAddStudent : Form
    {
        public frmAddStudent()
        {
            InitializeComponent();
        }

        // Declare variables to be used by both event handlers
        int CountStudents = 0;
        double Average = 0;
        Student[] ClassList = new Student[50];

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // Create new student and assign name etc provided by user
            Student _Student = new Student();
            _Student.Name = txtName.Text;
            _Student.Surname = txtSurname.Text;
            _Student.Age = Convert.ToInt32(txtAge.Text);
            _Student.ITMark = Convert.ToInt32(txtIT.Text);
            _Student.MathMark = Convert.ToInt32(txtEng.Text);
            _Student.EngMark = Convert.ToInt32(txtMath.Text);

            // Use the hasPassed method to display an appropriate message after the 
            // student has been added
            MessageBox.Show("Student added.");

            // Increase counter and display how many students added so far
            CountStudents++;

            Average = Convert.ToInt32(txtIT.Text) + Convert.ToInt32(txtEng.Text) + Convert.ToInt32(txtMath.Text);
            _Student.AverageMark = Average / 3;



            //Display Student's properties
            richTextBox1.Text += ("Student: " + Convert.ToString(CountStudents) +
                                 "\nName: " + _Student.Name +
                                 "\nSurname: " + _Student.Surname +
                                 "\nAge: " + _Student.Age +
                                 "\nStudent Average: " + Convert.ToString(_Student.AverageMark) + "\n" + "\n");


            //Add the newly added student to the ClassList array
            ClassList[CountStudents - 1] = _Student;

            //Clear the list
            txtSurname.Clear();
            txtName.Clear();
            txtAge.Clear();
            txtIT.Value = 0;
            txtEng.Value = 0;
            txtMath.Value = 0;
            txtName.Focus();
        }




        private void displayAll_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            for (int j = 0; j < ClassList.Count(); j++)
            {
                richTextBox1.Text += ("Student: " + Convert.ToString(j) +
                                     "\nName: " + ClassList[j].Name +
                                     "\nSurname: " + ClassList[j].Surname +
                                     "\nAge: " + ClassList[j].Age +
                                     "\nStudent Average: " + Convert.ToString(ClassList[j].AverageMark) +
                                     "\n" + "\n");
            }

            //MessageBox.Show("Display all students.");
        }
    }
}
JBTGE
  • 15
  • 3
  • 1
    What *is* on "line 13"? I suspect the issue is that one of the `txt*` variables evaluates to `null` and the issue has nothing to do with the array (or creating Student objects). – user2864740 Aug 21 '14 at 22:45
  • what line in your code is throwing the Null Reference Exception? not the line where the array is initialized? – Claies Aug 21 '14 at 22:46
  • See http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it?rq=1 (Because finding out exactly *where* an NRE/NPE comes is an invaluable lesson and relevant, if not boring, exercise in programming - *don't forget that a debugger can be attached*; check out my answer in the linked post) – user2864740 Aug 21 '14 at 22:48
  • if you don't actually need an array then a `List` would be easier to work with – Klors Aug 21 '14 at 22:49

2 Answers2

4

I suspect your null reference is being thrown in the displayAll_Click method. In that method you iterate over all the elements in the array (i.e. all 50), whether they have a student or not. This is because the Count() returns the length of the array, not the count of array elements that have a Student in them.

You should use either your student count variable CountStudents, in the for loop, or better yet, use List<Student>, which is ideal for your use, and less prone to these problems than an array.

1

What is "Count()" in the loop:

ClassList.Count();

Did you mean ClassList.Length;?

If you are populating the array dynamically, it's better to use List<Student> ClassList you get less problems with it.

Sqrs
  • 715
  • 5
  • 5