Odd Null Pointer

Hello,
I am getting a odd null pointer:
[java]at java.util.concurrent.ConcurrentHashMap.hash(ConcurrentHashMap.java:332)
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:1015)
at com.jme3.bullet.PhysicsSpace.addRigidBody(PhysicsSpace.java:550)
at com.jme3.bullet.PhysicsSpace.addCollisionObject(PhysicsSpace.java:391)
at com.jme3.bullet.control.RigidBodyControl.setPhysicsSpace(RigidBodyControl.java:241)
at com.jme3.bullet.PhysicsSpace.add(PhysicsSpace.java:373)
at lcs.electron.client.Communication.loadCell(Communication.java:301)
at lcs.electron.client.Window.updateCells(Window.java:251)
at lcs.electron.client.Window.updatePlayerPosition(Window.java:461)
at lcs.electron.client.Window.simpleUpdate(Window.java:224)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:242)
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:722)[/java]

In this code:

[java]Spatial b = loadBlock(ids[count]);
RigidBodyControl rbc = new RigidBodyControl(0.0f);
DataHandler.physics.getPhysicsSpace().add(rbc);
b.addControl(rbc);
cell.attachChild(b);[/java]

I am not sure what I am doing wrong, but apparently something!
Any help would be great, Thanks

1 Like

The line throwing the error in my code is:
[java]DataHandler.physics.getPhysicsSpace().add(rbc);[/java]

What is the nullpointer refering to?
Debug your code or do a System.out for every variable in that line to find out what is null.
Does [java]DataHandler.physics.getPhysicsSpace()[/java] return null?

I think the problem is here :

RigidBodyControl rbc = new RigidBodyControl(0.0f);

when you look at the javadoc, you have:

When using this constructor, the CollisionShape for the RigidBody is generated automatically when the Control is added to a Spatial.

So, when you add the rigidbodycontrol in the space, it has no collisionshape AND no spatial (cause the addControl call is made after).

I am unable to find what is null. Using a debugger every one of my variables look like they are good.
The problem occurs when this gets run, but neither the RigidBodyControl (rbc) nor the BulletAppState (DataHandler.physics) is null…
[java]
300: RigidBodyControl rbc = new RigidBodyControl(0.0f);
301: DataHandler.physics.getPhysicsSpace().add(rbc);
[/java]
[java]
java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.hash(ConcurrentHashMap.java:332)
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:1015)
at com.jme3.bullet.PhysicsSpace.addRigidBody(PhysicsSpace.java:550)
at com.jme3.bullet.PhysicsSpace.addCollisionObject(PhysicsSpace.java:391)
at com.jme3.bullet.control.RigidBodyControl.setPhysicsSpace(RigidBodyControl.java:241)
at com.jme3.bullet.PhysicsSpace.add(PhysicsSpace.java:373)
at lcs.electron.client.Communication.loadCell(Communication.java:301)
at lcs.electron.client.Window.updateCells(Window.java:251)
at lcs.electron.client.Window.updatePlayerPosition(Window.java:461)
at lcs.electron.client.Window.simpleUpdate(Window.java:224)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:242)
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:722)
[/java]

you didn’t understand me.

Try this:

[java]Spatial b = loadBlock(ids[count]);
RigidBodyControl rbc = new RigidBodyControl(0.0f);
b.addControl(rbc);
DataHandler.physics.getPhysicsSpace().add(rbc);
cell.attachChild(b);[/java]

@bubuche said: I think the problem is here :

RigidBodyControl rbc = new RigidBodyControl(0.0f);

when you look at the javadoc, you have:

So, when you add the rigidbodycontrol in the space, it has no collisionshape AND no spatial (cause the addControl call is made after).

Sorry, when I posted this had not updated, I did not see your post.

Sadly, I am still getting the same error… I now have:

[java]Spatial b = loadBlock(ids[count]);
RigidBodyControl rbc = new RigidBodyControl(0.0f);
b.addControl(rbc);
DataHandler.physics.getPhysicsSpace().add(rbc);
cell.attachChild(b);[/java]
[java]
java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.hash(ConcurrentHashMap.java:332)
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:1015)
at com.jme3.bullet.PhysicsSpace.addRigidBody(PhysicsSpace.java:550)
at com.jme3.bullet.PhysicsSpace.addCollisionObject(PhysicsSpace.java:391)
at com.jme3.bullet.control.RigidBodyControl.setPhysicsSpace(RigidBodyControl.java:241)
at com.jme3.bullet.PhysicsSpace.add(PhysicsSpace.java:373)
at lcs.electron.client.Communication.loadCell(Communication.java:359)
at lcs.electron.client.Window.updateCells(Window.java:251)
at lcs.electron.client.Window.updatePlayerPosition(Window.java:461)
at lcs.electron.client.Window.simpleUpdate(Window.java:224)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:242)
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:722)[/java]

Found it!

Apparently nodes do not deal well when controls are added to them…

@tlf30 said: Found it!

Apparently nodes do not deal well when controls are added to them…

??? Either you are wrong or you are not saying what you mean to say.

Of course nodes have no problem with controls.

An empty node with a physics control attached to it results in an empty physics control (no physics object is created).

OK, fixed the Node throwing the Null (Don’t know what I did, it just started working as I added other nodes), but now it is bouncing…
My player object is bouncing up and down on the ground and I am not sure why. I have not told it to (I don’t think)…

[java]DataHandler.players.attachChild(new Node(“Player”));
Spatial player = assetManager.loadModel(“Models/basic_man/basic_man.j3o”);
DataHandler.playerPhysics = new BetterCharacterControl(1.5f, 2.5f, 100);
DataHandler.playerPhysics.setGravity(new Vector3f(0f, -9.81f, 0f));
DataHandler.playerPhysics.setJumpForce(new Vector3f(0f, 100f, 0f));
DataHandler.players.getChild(“Player”).addControl(DataHandler.playerPhysics);
DataHandler.physics.getPhysicsSpace().add(DataHandler.playerPhysics);
DataHandler.playerNode = (Node) DataHandler.players.getChild(“Player”);
Material playerMat = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);
playerMat.setBoolean(“UseMaterialColors”, true);
ColorRGBA c = ColorRGBA.randomColor();
playerMat.setColor(“Ambient”, c);
playerMat.setColor(“Diffuse”, c);
player.setMaterial(playerMat);
DataHandler.playerNode.setLocalTranslation(0f, 5f, 0f);
DataHandler.playerNode.attachChild(player);
cam.setLocation(new Vector3f(0f, 5f, 20f));
rootNode.attachChild(DataHandler.playerNode);[/java]

I optimized the code just a touch, still don’t know what is wrong. I am unable to change the height or speed of the bounce but adjusting the arguments of the BetterCharacterControl.

[java]DataHandler.players.attachChild(new Node(DataHandler.playersData.get(playerIndex).id + “”));
DataHandler.player.playerNode = (Node) DataHandler.players.getChild(“Player”);
Spatial player = assetManager.loadModel(“Models/basic_man/basic_man.j3o”);
DataHandler.player.playerPhysics = new BetterCharacterControl(0.5f, 1.5f, 10f);
DataHandler.player.playerPhysics.setGravity(new Vector3f(0f, -9.81f, 0f));
DataHandler.player.playerPhysics.setJumpForce(new Vector3f(0f, 100f, 0f));
DataHandler.player.playerNode.addControl(DataHandler.player.playerPhysics);
DataHandler.physics.getPhysicsSpace().add(DataHandler.player.playerPhysics);
Material playerMat = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);
playerMat.setBoolean(“UseMaterialColors”, true);
ColorRGBA c = ColorRGBA.randomColor();
playerMat.setColor(“Ambient”, c);
playerMat.setColor(“Diffuse”, c);
player.setMaterial(playerMat);
DataHandler.player.playerNode.attachChild(player);[/java]