Nullpointerexception from rigidbodycontrol.setGravity()

I’m trying to use the setGravity method of RigidBodyControl. I’m getting a null pointer exception when I make the call. It looks like the method converts the jme3 Vector3f to a vecmath Vector3f then calls the RigidBody setGravity method. Looks like that’s where the exception is coming from. I’m using the nightly build from 3/17.



java.lang.NullPointerException

at com.jme3.bullet.objects.PhysicsRigidBody.setGravity(PhysicsRigidBody.java:352)

at Hello.HelloCollision.makeCannonBall(HelloCollision.java:310)

at Hello.HelloCollision.simpleUpdate(HelloCollision.java:358)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:241)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:158)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:203)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:221)

at java.lang.Thread.run(Unknown Source)





Here’s the code that generates the exception. The “gravity” vector is a jme3 Vector3f.



Vector3f gravity = new Vector3f(0,0,-40);

ball_phy.setGravity(gravity);

You probably used an empty constructor for the RigidBodyControl or try to use setGravity() after you created the RigidBodyControl with the mass-only constructor but before you attach it to the spatial. In both cases the “real” physics object isn’t created yet because no collision shape can be determined. I should probably throw a more explicit Exception in that case…

Ah, I was indeed using the mass-only constructor and calling setGravity prior to adding it to a spatial. That problem is fixed. Thanks. Now a follow-up question. What exactly does this method do? I was wanting to add an extra gravitational force to these objects, but setting different vectors into the call doesn’t seem to change anything.

It does, as stated in the javadoc, the gravity of the physicsspace is set to the single objects when they are added, thats default bullet behavior. You can change the gravity of single objects after adding or change the gravity of the physicsspace before you add them.

Gotcha. Thanks for your help.