Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking to set a leader from an array of sprites, I keep on getting a NullReferenceException was unhandled error from within my main game class when calling the UpdateMouse Method. What have I dont wrong here?

class MouseSprite

    Random random = new Random();
    private MouseSprite leader;

    public void UpdateBoundaryBox()
        mouseBounds.X = (int)mousePosition.X - mouseTexture.Width / 2; mouseBounds.Y = (int)mousePosition.Y - mouseTexture.Height / 2;

    public void UpdateMouse(Vector2 position, MouseSprite [] mice, int numberMice, int index)

        Vector2 catPosition = position;
        int enemies = numberMice;
        this.alive = true;

        mice[random.Next(0, mice.Length)] = leader;
share|improve this question

closed as too localized by Byte56, Trevor Powell, Josh Petrie, Laurent Couvidou, bummzack Dec 16 '12 at 13:08

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

You will benefit greatly from this post: How to use a debugger. – Byte56 Dec 8 '12 at 16:36
up vote 0 down vote accepted

leader is uninitialized. I assume you're trying to pick a random leader, not set the leader to some mouse randomly (overwriting the mouse stored there with null). You want to swap your assignment statement.

leader = mice[random.Next(0, mice.Length)];
share|improve this answer
random value is calculated right... it returns a number between 0 and mice.Lenght-1, remmeber that min value is inclusive and max value is exclusive... – Blau Dec 8 '12 at 16:33
Ah, true enough, I'm used to inclusive randoms. – Byte56 Dec 8 '12 at 16:34
even swapping them around I still get the same error – Craig Dec 8 '12 at 16:46
sorry it did fix it, thanks! – Craig Dec 8 '12 at 16:52

Null reference exceptions are thrown because you are trying to access to a reference typed variable that is not assigned.

In the UpdateMouse method code, the only candidate to throw that exception is the array, that maybe is not assigned, check it.

share|improve this answer
This is a symptom, not a solution. While it may be obvious to you or I how to fix this, this answer should still include a solution. As it stands, it's just a suggestion on what's wrong, instead of how to fix it. So more of a comment. – Byte56 Dec 10 '12 at 19:12
"Check it" is not a solution... – Byte56 Dec 10 '12 at 23:45
:P ... the question is "What have I dont wrong here?" Mine is a simple answer that complements yours. If you see the code and where is throwed the exception... is the more reasonable... your answer is more heuristic and other approach to solution... is funny that I could downvote you by an objetive fail in your answer... and you downvote me by subjetive criteria... – Blau Dec 11 '12 at 9:27
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Laurent Couvidou Dec 11 '12 at 10:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.