Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to detect a collision between an AnimatedSprite and the Rectangle floor. When they collide, I want the sprite to be removed. However, I keep getting a null pointer error. Am I doing this the wrong way?

Inside my add face function. face is an AnimatedSprite created, and ground is a rectangle.

this.mScene.registerUpdateHandler(new IUpdateHandler() {
            @Override
            public void reset() { }

            @Override
            public void onUpdate(final float pSecondsElapsed) {
                    if(face.collidesWith(ground)) {
                           Debug.d("Contact!");
                           removeFace(face);       
                    } 
            }
    });     

removeFace() is:

private void removeFace(final AnimatedSprite face) {
    final PhysicsConnector facePhysicsConnector = this.mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(face);

    this.mPhysicsWorld.unregisterPhysicsConnector(facePhysicsConnector);
    this.mPhysicsWorld.destroyBody(facePhysicsConnector.getBody());

    this.mScene.unregisterTouchArea(face);
    this.mScene.detachChild(face);

    System.gc();
}

The null pointer exception is inside removeFace()

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Look at the stack trace it will tell you exactly which line has the null pointer exception. You need to invest in learning to use the debugger too, these are simple problems to debug and you can solve your own problems much faster than waiting for a reply here.

From your code I'm assuming that

this.mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(face)

is returning null because it's unable to find the face you specify. So it could easily throw a null pointer exception when it reaches: facePhysicsConnector.getBody(). Since you can't access a method of a null object.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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