NullPointerException in PhysicsSpace

Hello,
sometimes when playing my game, I get a NPE in the PhysicsSpace. This is the Exception:

Jan 24, 2015 12:28:47 AM com.jme3.app.Application handleError
Schwerwiegend: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.bulletphysics.collision.dispatch.CollisionWorld$RayResultCallback.needsCollision(CollisionWorld.java:673)
at com.bulletphysics.collision.dispatch.CollisionWorld.rayTest(CollisionWorld.java:555)
at com.jme3.bullet.PhysicsSpace.rayTest(PhysicsSpace.java:714)
at com.jme3.bullet.control.BetterCharacterControl.checkOnGround(BetterCharacterControl.java:468)
at com.jme3.bullet.control.BetterCharacterControl.prePhysicsTick(BetterCharacterControl.java:166)
at com.jme3.bullet.PhysicsSpace$3.internalTick(PhysicsSpace.java:259)
at com.bulletphysics.dynamics.DiscreteDynamicsWorld.internalSingleStepSimulation(DiscreteDynamicsWorld.java:365)
at com.bulletphysics.dynamics.DiscreteDynamicsWorld.stepSimulation(DiscreteDynamicsWorld.java:339)
at com.jme3.bullet.PhysicsSpace.update(PhysicsSpace.java:330)
at com.jme3.bullet.PhysicsSpace.update(PhysicsSpace.java:317)
at com.jme3.bullet.BulletAppState.render(BulletAppState.java:257)
at com.jme3.app.state.AppStateManager.render(AppStateManager.java:300)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:251)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:744)

This is the line of code in PhysicsSpace.rayTest which is causing the issue:

dynamicsWorld.rayTest(Converter.convert(from, rayVec1), Converter.convert(to, rayVec2), new InternalRayListener(results));

The problem obviously occurs when I do physics stuff, e.g. when I attach a new RigidBodyControl for a grenade or when I enable the ragdoll physics. The strange thing is that it seems to be completely random, and usually it does not happen, but as I said, sometimes the applicaton exits and I have that error…

…but is it the line in needsCollision()? Because that’s where the real issue is.

com.bulletphysics.collision.dispatch.CollisionWorld$RayResultCallback.needsCollision(CollisionWorld.java:673)

I don’t have that source code in front of me so I can’t tell whether your post is wrong or which line you think was important is wrong. CollisionWorld.java:673 is the line you want to post. It looks like you are actually two levels up from that by showing the rayTest() call… which itself isn’t relevant exactly.

https://code.google.com/p/jbullet-jme/source/browse/branches/jbullet/src/com/bulletphysics/collision/dispatch/CollisionWorld.java#673

                public boolean needsCollision(BroadphaseProxy proxy0) {
                        boolean collides = ((proxy0.collisionFilterGroup & collisionFilterMask) & 0xFFFF) != 0; // <-- 673
                        collides = collides && ((collisionFilterGroup & proxy0.collisionFilterMask) & 0xFFFF) != 0;
                        return collides;
                }

If you can try to replace jbullet.jar by jmbullet-0.3.0.jar (Because I’ve got the source on github, and I’ll be sure of the line number in the stacktrace)

Well, it seems reasonable that it might be the line since there is only one possible way (in that code) that needsCollision() could throw an NPE… if proxy0 is null.

…then one needs to trace back up and find out why proxy0 is null.

From my quick search, it can happen when you try to access an object that is removed from the physics space.

Because you say, it happens sometimes. I suspect you call removeRigidBody, or removeCollisionObject,… from an other Thread than the Thread that run physics.

Thanks guys for finding the correct piece of code… In the SDK, I just had the option to view the source of PhysicsSpace so I just pasted it, I didn’t think of searching on google or github for the real line of code :confused:

As for threading, this is the only thing I do on other threads:

app.getStateManager().getState(GameRunningState.class).getPhysicsManager().add(ragdoll);
            enemy.setCullHint(Spatial.CullHint.Never);
            ragdoll.setRagdollMode();

The PhysicsManager is just a wrapper for a BulletAppState and its Add method calls this:

bulletAppState.getPhysicsSpace().add(o);

The problem is that I can’t really test if this is the actual issue. It works most of the times with the code I have, but sometimes I get the exception…

EDIT: I now just do everything on the main thread, and if the issue appears again, I’m gonna post it here.

Are you by chance interacting with bullet from multiple threads ? (aka anything not from the renderer thread?)

Well, the only thing I didn’t do on the renderer thread was the thing I posted in the post above, but I now also do that on the main thread. Otherwise, my game doesn’t have any networking code and I don’t do anything off of the main thread.