nullpointerexception - Libgdx null Pointer -
i´m working on box2d jump , run game , want player teleported start when hits spike, when hits spike, null pointer exception.
heres 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 part of 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); 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); }
and error is:
exception in thread "lwjgl application" java.lang.nullpointerexception @ de.tobls.hardwarerunner.sprites.player.die(player.java:223) @ de.tobls.hardwarerunner.tools.worldcontactlistener.begincontact(worldcontactlistener.java:27) @ com.badlogic.gdx.physics.box2d.world.begincontact(world.java:982) @ com.badlogic.gdx.physics.box2d.world.jnistep(native method) @ com.badlogic.gdx.physics.box2d.world.step(world.java:686) @ de.tobls.hardwarerunner.screens.playscreen.update(playscreen.java:112) @ de.tobls.hardwarerunner.screens.playscreen.render(playscreen.java:127) @ com.badlogic.gdx.game.render(game.java:46) @ de.tobls.hardwarerunner.hardwarerunner.render(hardwarerunner.java:71) @ com.badlogic.gdx.backends.lwjgl.lwjglapplication.mainloop(lwjglapplication.java:215) @ com.badlogic.gdx.backends.lwjgl.lwjglapplication$1.run(lwjglapplication.java:120)
can me?
first of should not destroy worlds items in asynchronous methods - notice contact listeners's methods asynchronous.
the proper way destroy body od mark , destroy right before world.update()
//in asynchronous method body.setuserdata("destroy"); //when rendering for(body body : world.getbodies()) if(body.getuserdata().equals("destroy")) world.destroybody(body);
but on top of - see doing nothing moving body when die why not use settransform method then
body.settransform(new vector2(newx, newy), body.getangle() );
instead of removing body , creating again in new position
Comments
Post a Comment