I have a button on form1 that does this:
private void button1_Click(object sender, EventArgs e)
{
int numberOfStudentsInClass;
if (!int.TryParse(txtBoxNoS.Text, out numberOfStudentsInClass))
{
MessageBox.Show("Please enter the number of students in the class", "Alert", MessageBoxButtons.OK);
return;
}
_allStudentsArray = new Student[numberOfStudentsInClass];
form3.Show();
form3.lblClass.Text = string.Format("Class {0}", comboBox1.Text);
form3.lblNoOfS.Text = numberOfStudentsInClass.ToString();
}
form3 is correctly passing the right ints and strings to this function:
public void addStudent(string sn, string tp, string tw, string gc, string gp, string gg, string t, string n)
{
int sni = int.Parse(sn) - 1;
_allStudentsArray[sni] = new Student();
_allStudentsArray[sni].studentNumber = int.Parse(sn);
_allStudentsArray[sni].topics = tp;
_allStudentsArray[sni].testWeek = tw;
_allStudentsArray[sni].gradeContent = int.Parse(gc);
_allStudentsArray[sni].gradePrn = int.Parse(gp);
_allStudentsArray[sni].gradeGrammar = int.Parse(gg);
_allStudentsArray[sni].total = int.Parse(t);
_allStudentsArray[sni].notes = n;
}
At the start of the namespace I have:
public class Student
{
public int studentNumber;
public string topics;
public string testWeek;
public int gradeContent;
public int gradeGrammar;
public int gradePrn;
public int total;
public string notes;
public int studentsInClass;
public int lastTestWeek;
}
Student[] _allStudentsArray;
But as soon as I try to assign _allStudentArray[sni].studentNumber = sn
I get a null exception error. I've read the very detailed answer on null exceptions and I think it's coming from the array not being initialized correctly. But for 2 days I haven't been able to figure out why.