Nullpointer when updating physics. [SOLVED]

EDIT: I just realized this shouldn’t have been posted in this section of the forum, sorry about that.

I’m using code from one of the tutorials on collisions and physics. What I do is I have a method to initialize the scene and one for updating it in my main class. I call these methods from a control class for nifty. The initialize part works just fine, however the update part returns a null pointer. Here is my code.


public class GuiGameBase extends SimpleApplication implements ActionListener,ScreenController{

…//unrelated code

public void gameStartSettings(){

bulletAppState = new BulletAppState();


// Re-use the flyby camera for rotation, while positioning is handled by physics

viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));




// Load scene from zip file.

assetManager.registerLocator(“”, ZipLocator.class.getName());

sceneModel = assetManager.loadModel(“main.scene”);


// Create compound collision shape for scene.

CollisionShape sceneShape =

CollisionShapeFactory.createMeshShape((Node) sceneModel);

landscape = new RigidBodyControl(sceneShape, 0);


//Set up crosshairs


// Set character collision and physics.

CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);

player = new CharacterControl(capsuleShape, 0.05f);




player.setPhysicsLocation(new Vector3f(0, 10, 0));

// Attach scene and player to root node and physics space.





public void onGameUpdate(){

Vector3f camDir = cam.getDirection().clone().multLocal(charspeed); // <

Null Pointer Exception
Vector3f camLeft = cam.getLeft().clone().multLocal(charspeed);
walkDirection.set(0, 0, 0);
if (left) { walkDirection.addLocal(camLeft); }
if (right) { walkDirection.addLocal(camLeft.negate()); }
if (up) { walkDirection.addLocal(camDir); }
if (down) { walkDirection.addLocal(camDir.negate()); }

......//More unrelated code.


Now, I call these in the update of my other class.

package gamerScreen;

import com.jme3.asset.AssetManager;
import com.jme3.input.FlyByCamera;
import de.lessvoid.nifty.Nifty;
import com.jme3.renderer.Camera;
import com.jme3.font.BitmapFont;
import com.jme3.input.InputManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.system.AppSettings;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class HudScreen extends AbstractAppState implements ScreenController {

private Nifty nifty;
private GuiGameBase base = new GuiGameBase().getInstance();
private Screen screen;
private AssetManager assetManager;
private FlyByCamera flyCam;
private Node rootNode;
private Node guiNode;
private ViewPort guiViewPort;
private ViewPort viewPort;
private AppSettings settings;
private AppStateManager stateManager;
private BitmapFont guiFont;
private InputManager inputManager;
private boolean gameStart = false;
private boolean gameUpdate = false;
private SimpleApplication app;

/** Nifty GUI ScreenControl methods */

public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;

public void onStartScreen() {

public void onEndScreen() { }

public void startGame(){


public void changeScreen(String nextScreen){


public void gameExit(){


/** jME3 AppState methods */

public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);;
assetManager = app.getAssetManager();
guiViewPort = app.getGuiViewPort();

public void checkScreen(){
if(nifty.getCurrentScreen() == screen){
gameStart = true;


public void update(float tpf) {
/** jME update loop! */
guiNode = app.getGuiNode();
rootNode = app.getRootNode();
flyCam = app.getFlyByCamera();
gameStart = false;
gameUpdate = true;


The problem is probably something simple, but after two hours of searching around I figured I'd finally come to you guys. If I call just base.gameStartSettings() the scene starts fine, but I have no weight/gravity or collision box. So then I have to call base.onGameUpdate() and I get a nullpointer exception. (and charspeed is set);

Make sure the cam and charspeed objects aren’t null.

I know char speed is set, and I don’t see how the cam would not be set. I’ve tried everything I can to make sure the camera is set. I’ve tried a few things but none of them worked. I’ve tried changing cam.getDirection(). to (gBase is an object of GuiGameBase) and got the same error. Also passing in a camera from my HudScreen class and that gave me the same error. I’ve run out of ideas to try.

Be more specific. Where does it crash? What is the exact output (to find the the line)? Is cam null or not?

Normally, cam is initialized in the Application base class and then taken by SimpleApplication for the flyCam. So try flyCam.getDirection or use getCamera() and report back.

Where does it crash? What is the exact output (to find the the line)? Is cam null or not?

I tried the flyCam.getDirection but flyCam does not have a getDirection method and getCamera() just returned the same error on the same line.

(I didn't provide the full code for GuiGameBase so the error actually occurs on line47 of the code I gave. If you need the full code just say so. I didn't provide it all because it's length and most of it is unrelated to this issue I believe. )
[xml]SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
at gamerScreen.GuiGameBase.onGameUpdate(
at gamerScreen.HudScreen.update(
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(

Line 47? It says

[java] walkDirection.set(0, 0, 0);[/java]

Is walkDirection instantiated?

I meant line 45, typo on my part.

It’s the cam that is null for sure but I can’t seem to make it not null and flyCam doesn’t have the methods cam does so I can’t use it. I tried to pass in the cam from HudScreen and I tried using getCamera() and it still said null.

Maybe your state wasn’t initialized yet!?

And here comes the noob question, what do you mean by my state not being initialized yet?

Fixed it, I needed viewPort.getCamera() instead of cam.