private void btnAddStudent_Click(object sender, EventArgs e)
{
student[counter] = new Student(txtStudentName.Text, txtStudentSurname.Text, int.Parse(txtExamMark.Text), counter);
counter++;
}
private void btnAverage_Click(object sender, EventArgs e)
{
for (int i = counter; i <= counter; i++)
MessageBox.Show("" + student[i].Average);
}
My program is giving me the error:
Object reference not set to an instance of an object.
I only want the loop to run once to only display the last calculated average. If i do this:
ie: change int i = counter
to i = 0
private void btnAverage_Click(object sender, EventArgs e)
{
for (int i = 0; i < counter; i++)
MessageBox.Show("" + student[i].Average);
}
Then my program works but it displays the messagebox as many times depending on the amount of students i
entered, with the last value being the correct average.
I used a class called Student
to calculate the average. That is not the problem however, because the correct average is being displayed.
What can I do to fix this error?