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.

What's up everyone,

I am heuristically producing a Pong clone in Box2D via libGDX. The Null Pointer Exception is originating in the ContactListener's beginContact() method in which I am trying to implement score logic.

The Box2D world uses two separate EdgeShapes as sensors for goals that increment a score variable upon collision with the Ball. The collision logic between the Ball and the two EdgeShapes works, but collision between the Ball and anything else in the Box2D world crashes the program.

The stack trace:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.ckq3r.Pong.screens.GameScreen$2.beginContact(GameScreen.java:491)
at com.badlogic.gdx.physics.box2d.World.beginContact(World.java:876)
at com.badlogic.gdx.physics.box2d.World.jniStep(Native Method)
at com.badlogic.gdx.physics.box2d.World.step(World.java:602)
at com.ckq3r.Pong.screens.GameScreen.render(GameScreen.java:99)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.ckq3r.Pong.PongGame.render(PongGame.java:236)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:204)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:112)

The problematic code:

    /**Box2D contact listener**/
private void createContactListener() {
    world.setContactListener(new ContactListener() {

        @Override
        public void beginContact(Contact contact) {
            Fixture fixtureA = contact.getFixtureA();
            Fixture fixtureB = contact.getFixtureB();

            Gdx.app.log("beginContact", "between " + fixtureA.toString() + " and " + fixtureB.toString());

            if(fixtureA.getBody().getUserData().equals(1) && fixtureB.getBody().getUserData().equals(2) || fixtureA.getBody().getUserData().equals(2) && fixtureB.getBody().getUserData().equals(1)){
                Gdx.app.log("HIT", "goal1 contact");
                score1++;
                score1String = score1 + "";
            }

            if(fixtureA.getBody().getUserData().equals(1) && fixtureB.getBody().getUserData().equals(3) || fixtureA.getBody().getUserData().equals(3) && fixtureB.getBody().getUserData().equals(1)){
                Gdx.app.log("HIT", "goal2 contact");
                score2++;
                score2String = score2 + "";
            }
        }

        @Override
        public void endContact(Contact contact) {
            Fixture fixtureA = contact.getFixtureA();
            Fixture fixtureB = contact.getFixtureB();
            Gdx.app.log("endContact", "between " + fixtureA.toString() + " and " + fixtureB.toString());
        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {
        }

        });
} 

Notes:

  1. When I comment out the two conditional statements within the beginContact() method, the code runs. When uncommented, the error is reproduced.
  2. The Ball userData is circleBody.setUserData(1);
  3. The goal1 userData is goalBody.setUserData(2);
  4. The goal2 userData is goalBody.setUserData(3);
share|improve this question
    
it's either fixtureA.getBody(), fixtureA.getBody().getUserData(), fixtureB.getBody().getUserData() or fixtureA.getBody() just check which one is null and ignore that contact. –  PeterT Jul 27 '13 at 19:56
    
@PeterT Thanks for your time. I solved it. I made the newbie mistake of not setting the userdata of the other objects within the world. –  HACKQ3R Jul 27 '13 at 20:12

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.