NullPointerException in PhysicsSpace

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

Jan 24, 2015 12:28:47 AM handleError
Schwerwiegend: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
at com.bulletphysics.collision.dispatch.CollisionWorld$RayResultCallback.needsCollision(
at com.bulletphysics.collision.dispatch.CollisionWorld.rayTest(
at com.jme3.bullet.PhysicsSpace.rayTest(
at com.jme3.bullet.control.BetterCharacterControl.checkOnGround(
at com.jme3.bullet.control.BetterCharacterControl.prePhysicsTick(
at com.jme3.bullet.PhysicsSpace$3.internalTick(
at com.bulletphysics.dynamics.DiscreteDynamicsWorld.internalSingleStepSimulation(
at com.bulletphysics.dynamics.DiscreteDynamicsWorld.stepSimulation(
at com.jme3.bullet.PhysicsSpace.update(
at com.jme3.bullet.PhysicsSpace.update(
at com.jme3.bullet.BulletAppState.render(
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(

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.


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. 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.

                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:


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


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.