Search code examples
javanullpointerexceptionlibgdxbox2d

Libgdx box2D error


I´m working on a Box2D Jump and run game and want the player to die when he hits a spike, but when he hits the spike, I get a null pointer exception.

Heres my Contact class:

public class WorldContactListener implements ContactListener {

Player player;

@Override
public void beginContact(Contact contact) {
    Fixture fixA = contact.getFixtureA();
    Fixture fixB = contact.getFixtureB();
    player = new Player();

    int cDef = fixA.getFilterData().categoryBits | fixB.getFilterData().categoryBits;

    switch (cDef) {
        case HardwareRunner.PLAYER_BIT | HardwareRunner.BRICK_BIT:
        case HardwareRunner.PLAYER_BIT | HardwareRunner.SPIKE_BIT:
            player.die();

    }
}

And heres the part of my player class:

public void definePlayer(){
    bdef.position.set(32 / runner.PPM, (6 * 32) / runner.PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);

    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();
    Vector2[] vertice = new Vector2[4];
    vertice[0] = new Vector2(-13, 13).scl(1 / runner.PPM);
    vertice[1] = new Vector2(13, 13).scl(1 / runner.PPM);
    vertice[2] = new Vector2(13, -13).scl(1 / runner.PPM);
    vertice[3] = new Vector2(-13, -13).scl(1 / runner.PPM);
    shape.set(vertice);
    shape.getRadius();
    fdef.filter.categoryBits = HardwareRunner.PLAYER_BIT;
    fdef.filter.maskBits =
            HardwareRunner.GROUND_BIT |
            HardwareRunner.COIN_BIT |
            HardwareRunner.BRICK_BIT |
            HardwareRunner.ENEMY_BIT |
            HardwareRunner.SPIKE_BIT |
            HardwareRunner.ENEMY_HEAD_BIT |
            HardwareRunner.ITEM_BIT;

    fdef.shape = shape;
    fdef.friction = .1f;
    b2body.createFixture(fdef).setUserData(this);
    fdef.isSensor = true;

    b2body.createFixture(fdef).setUserData(this);
}

public void die() {
    world.destroyBody(b2body);
}

And the error is:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at de.tobls.hardwarerunner.Sprites.Player.die(Player.java:223)
at de.tobls.hardwarerunner.Tools.WorldContactListener.beginContact(WorldContactListener.java:27)
at com.badlogic.gdx.physics.box2d.World.beginContact(World.java:982)
at com.badlogic.gdx.physics.box2d.World.jniStep(Native Method)
at com.badlogic.gdx.physics.box2d.World.step(World.java:686)
at de.tobls.hardwarerunner.Screens.PlayScreen.update(PlayScreen.java:112)
at de.tobls.hardwarerunner.Screens.PlayScreen.render(PlayScreen.java:127)
at com.badlogic.gdx.Game.render(Game.java:46)
at de.tobls.hardwarerunner.HardwareRunner.render(HardwareRunner.java:71)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)

Can anyone help me?


Solution

  • The problem comes from the fact that when your player dies, you destroy the body in the beginContact(Contact contact). That's a pretty classic problem with box2D. During the simulation step, box2D will check if there is a collision between every bodies, but if you destroy a body while the simulation is running, it will give you this NullPointerException.

    To workaround this problem, you could use a boolean, let's say the boolean dead. in your beginContact(Contact contact) you would have :

    switch (cDef) {
        case HardwareRunner.PLAYER_BIT | HardwareRunner.BRICK_BIT:
        case HardwareRunner.PLAYER_BIT | HardwareRunner.SPIKE_BIT:
            player.dead = true;
    }
    

    And in the render() method of you'll check if the player is dead, and destroy the body :

    if(player.dead)
        player.die();