TotalJargon
TotalJargon

Reputation: 147

Array of an Object error accessing variables: NullReferenceException was unhandled by user code

Ok so I'm attempting to create a high score system and am using an array of a class within an object, however the error in the title appears which tells me that for some reason my objects are full of null variables, here are my classes:

class HighScores
{
    public HighScore[] MyScores = new HighScore[5];

    protected bool failedRead = false;

    private void SortHighScores(string NewName, int NewScore)
    {
        HighScore[] TmpScores = new HighScore[6];

        for (int i = 0; i < 5; i++)
        {
            TmpScores[i].Name = MyScores[i].Name; // Error Occurs here for `MyScores[i].Name` however in output box both MyScores and TmpScores show null
            TmpScores[i].Score = MyScores[i].Score;
        }
        TmpScores[5].Name = NewName;
        TmpScores[5].Score = NewScore;

        Array.Sort(TmpScores, delegate(HighScore x, HighScore y) { return x.Score.CompareTo(y.Score); });
        Array.Reverse(TmpScores);

        MyScores = new HighScore[5];
        for (int i = 0; i < 5; i++)
        {
            MyScores[i].Name = TmpScores[i].Name;
            MyScores[i].Score = TmpScores[i].Score;
        }
    }
}

class HighScore
{
    public string Name = "hello world";
    public int Score = 0;
}

And here is where I initialise an object of HighScores:

HighScores GameScores;
protected override void Initialize()
{
    base.Initialize();
    GameScores = new Highscores();
}

Surely the objects shouldn't be null if I made them as new?

[EDIT]

OK so I've made a new function within the class HighScores to initialise the MyScores objects as new HighScore objects:

public void InitScores()
{
    for (int i = 0; i < 5; i++)
    {
        MyScores[i] = new HighScore();
    }
}

Which is called within protected override void Initialize() (I'm sure you're all familiar with it, it's standard in XNA). I also have added the same code in the class function: GameScores.SortHighScores to initialise TmpScores.

Still the same error, still the same place where it occurs, any ideas?

Upvotes: 0

Views: 311

Answers (2)

Steve
Steve

Reputation: 216293

The initialization of GameScores isn't enough. You don't initialize the values for the internal array of MyScores (at least not in the code shown). Of course when you try to use any element of that array you get a NullReferenceException

You need to add in the constructor of HighScores a code similar to this

public HighScores()
{
    MyScores = new HighScore[5];
    for(x = 0; x < 5; x++)
        MyScores[i] = new HighScore();

}

EDIT:

This code has the same conceptual error

HighScore[] TmpScores = new HighScore[6];

for (int i = 0; i < 5; i++)
{
    TmpScores[i].Name = MyScores[i].Name;             
    TmpScores[i].Score = MyScores[i].Score;
}

Here you declare an array called TmpScores with space to store 6 elements of type HighScore, but these 6 elements are all null and so you can't assign to the variable Name or Score of an element (i) that is null.

Perhaps this code will work for you:

    for (int i = 0; i < 5; i++)
    {
        // Assign to tmpScores an already initialized object HighScore
        TmpScores[i] = MyScores[i];
    }

    TmpScores[5] = new HighScore {Name = NewName, Score = NewScore};

Upvotes: 1

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

It looks like you need to create the instances of HighScore for your MyScores array. You only create the array in the code above.

So somewhere in the code you need code similar to

MyScores[i] = new HighScore();

Upvotes: 2

Related Questions