1

I have a method caller addColisionBox and when i call it and setting values to it i get nullpointer at the place im calling it.. I will show some code:

    public void addCollisionBox(int x, int y, int arrayNum)
    {
        //Creating a new rectangle at the x & y cord passed in
        rectangle[arrayNum] = new Rectangle(x, y, R_Width, R_Height);
    }

And i created inside another class like this:

CollisionHandler collision;
....
//CurrentX and CurrentY position to pass into addCollisionBox method and at the array number i
collision.addCollisionBox(currentX, currentY, i);

And it says in a message box that Visual C# express give out that: "Object reference not set to an instance of an object."

4
  • 2
    you forget collision = new CollisionHandler(); Commented Dec 9, 2011 at 20:57
  • Oh im crazy.. How could i forget this.. Commented Dec 9, 2011 at 20:57
  • 1
    Unless there is code initializing the object between the declaration and the method call, you're getting the NullPointerException because you didn't initialize the object. Commented Dec 9, 2011 at 20:58
  • 1
    Note that in C#, it's a NullReferenceException, which is the functional equivalent of NullPointerException in Java. Commented Dec 9, 2011 at 20:59

5 Answers 5

6

You didn't initalize your collision object. You should have something similar to the following. e.g.

CollisionHandler collision = new CollisionHandler();

...or how ever you are creating/grabbing an instance of your object prior to using it.

Sign up to request clarification or add additional context in comments.

1 Comment

I wil accept this answear in 10 minutes.. I have to wait before accepting apparently.
2

You have not created another instance, all you have done is create a variable of a given type.

CollisionHandler collision = new CollisionHandler();
//                 ^ variable           ^ instance of object

Comments

1

You have not created an instance of your CollisionHandler object. Try something like this:

CollisionHandler collision = new CollisionHandler();
....
//CurrentX and CurrentY position to pass into addCollisionBox method and at the array number i
collision.addCollisionBox(currentX, currentY, i);

Comments

0

you need

CollisionHandler collision = new CollisionHandler();

You've just declared the variable but not set it to anything, hence the null reference exception.

Comments

0

I would suggest that collision is null when you're trying to call the addCollisionBox method on it, thereby causing the null dereference. If it definitely has a value at some stage then you're probably deleting it somewhere, but given the code you've pasted it seems more likely that you just need to create an instance of CollisionHandler as it doesn't appear that you're doing so.

CollisionHandler collision = new CollisionHandler();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.