0

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.

Rob
  • 25,569
  • 15
  • 73
  • 87
Luves2spooge
  • 78
  • 1
  • 8
  • 1
    It's *actually* failing on `_allStudentsArray[sni] = new Student();`. And this is happening because `_allStudentsArray` is not instantiated. I see that you are instantiating it, and then popping up a form, so my guess is that the *new* form is calling `addStudent`. Since the field is *local*, it means it's a different array. – Rob Apr 20 '16 at 01:24
  • These lines: `form3.lblClass.Text` indicate that you know you need to pass some information to the new form. You need to do the same with the array. – Rob Apr 20 '16 at 01:26
  • If you are interested in answer - provide [MCVE]. Otherwise the question will be closed as duplicate of "Fix NRE" as @DavidL recommends. – Alexei Levenkov Apr 20 '16 at 01:26
  • @Rob, ah that makes sense. So when I call `addStudent()` the data isn't passed to form1 that holds `addStudent()`, rather `addStudent()` is passed to form3? – Luves2spooge Apr 20 '16 at 02:09
  • @Luves2spooge It's not so much the methods that are changing, it's that each form has its own set of data. If you are familiar with OOP, each form is an *object*, which contains its own properties. Setting `form1._allStudentsArray` does not modify `form3._allStudentsArray`. – Rob Apr 20 '16 at 02:15
  • @Rob Okay. No, I'm not familiar with OOP, this is my first C# application and I only have a limited background in Javascript. Where I define `form3` can I pass the `_allStudentsArray` like `Form3 form3 - new Form3(_allStudentsArray)'? Would it then exist in Form3 and be usable? – Luves2spooge Apr 20 '16 at 02:52
  • @Luves2spooge Yes, assuming your constructor properly stored the array locally, it would work – Rob Apr 20 '16 at 02:52

0 Answers0