-1

Object reference not set to an instance of an object. I am still having the same issue...Student S is passed in and (Student)s.scores contains a string of "80 90 100"

    public Student GetUpdatedScores(Student s)
    {

        txtName.Text = s.Name;  
        lstScores.Items.Clear();
        string[] listOfScores = s.Scores.Split(' '); //receiving error on this line.
        for (int i = 0; i < (listOfScores.Length - 1); i++)
        {
            lstScores.Items.Add(listOfScores[i]);
        }
        this.ShowDialog();
        return student;
    }
Jerry G
  • 39
  • 6
  • 7
    Why on earth would you have multiple variables in the same method which vary only by case? That seems to me to be making the code hard to read just for the sake of it. Presumably `s.Scores` is null... there's not a lot else we can say. – Jon Skeet Mar 18 '14 at 20:40
  • 1
    To get this error, ```scoreS``` must be null, so your incoming ```s.Scores``` must also be null. – Aaron D Mar 18 '14 at 20:41
  • (being a newbie) after reading it, the only extra variable is namE...will remove it. – Jerry G Mar 18 '14 at 20:42
  • 1
    You also have scOres and scoreS – Darren Young Mar 18 '14 at 20:46
  • 1
    You would get that error if a method is operating on a `null` variable, so I presume `scoreS` is null. Check what `s.Scores` is, and check if it's `null`. Also, I highly recommend better names. To someone reading me (me), `scoreS` and `scOres` is not only confusing, but makes no sense. – theGreenCabbage Mar 18 '14 at 20:48

1 Answers1

3

Obviously enough s.Scores property returns a null value. When you affect it to scoreS string variable and then try to split it, you get your exception.

This kind of problem is easy to solve with a debugging session. Plus, if you want to make sure a value cannot be null without you knowing while you are debugging, use Debug.Assert calls. For example:

Debug.Assert(scoreS != null);

Just so you know these calls won't be compiled on release compilation config.

As an additional advice, if you don't mind me saying, you should not have variables which only differs by case. In fact, I believe you should drop both namE and scoreS as they're completely useless here. Work with the instance properties instead. This will make code easier to read and in the process help you figuring out issues like this one by yourself when they occur.

UPDATE:

Here is how I would have wrote it (with some comments explaining further):

// renamed the function to reflect what it really should do (and does)
public DialogResult ShowUpdatedScores(Student student)
{
    // since your method is public this is rather good practice
    if (student == null) throw new ArgumentNullException("student");

    // with this, scores cannot be null while you debug without you knowing
    Debug.Assert(student.Scores != null);

    // who needs loops when there's AddRange method? :)
    lstScores.Items.Clear();
    lstScores.AddRange(student.Scores.Split(' '));

    txtName.Text = student.Name;  

    // it's pointless to return the same student pointer that was passed as a parameter, returning DialogResult makes more sense
    return this.ShowDialog();
}

Of course that's making some assumptions about your code but you get the idea.

Crono
  • 10,211
  • 6
  • 43
  • 75
  • Advice is greatly appreciated as I am very new to the C# world. – Jerry G Mar 18 '14 at 21:04
  • @JerryG you are welcome. We've all been there. :) Since you seem to be new to StackOverflow too, here's another advice: whenever you ask a question and receive an helpful answer, mark it as the answer and/or vote it up. This makes your post marked as answered and it gives points to people who helps you. ;) – Crono Mar 18 '14 at 21:06