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.



GuiGameBase.java



[java]

public class GuiGameBase extends SimpleApplication implements ActionListener,ScreenController{

…//unrelated code



public void gameStartSettings(){



bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);



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

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

flyCam.setMoveSpeed(100);

setUpKeys();

setUpLight();



// Load scene from zip file.

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

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

sceneModel.setLocalScale(2f);



// Create compound collision shape for scene.

CollisionShape sceneShape =

CollisionShapeFactory.createMeshShape((Node) sceneModel);

landscape = new RigidBodyControl(sceneShape, 0);

sceneModel.addControl(landscape);



//Set up crosshairs

initCrossHairs();



// Set character collision and physics.

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

player = new CharacterControl(capsuleShape, 0.05f);

player.setJumpSpeed(charjump);

player.setFallSpeed(100);

player.setGravity(80);

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



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

rootNode.attachChild(sceneModel);

bulletAppState.getPhysicsSpace().add(landscape);

bulletAppState.getPhysicsSpace().add(player);



}



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()); }
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
}


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

[/java]

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

[java]
package gamerScreen;


import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
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() {
checkScreen();
}

public void onEndScreen() { }

public void startGame(){

}

public void changeScreen(String nextScreen){
nifty.gotoScreen(nextScreen);

}

public void gameExit(){
nifty.exit();
System.exit(0);

}



/** jME3 AppState methods */

@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app=(SimpleApplication)app;
assetManager = app.getAssetManager();
guiViewPort = app.getGuiViewPort();
}

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

}

@Override
public void update(float tpf) {
/** jME update loop! */
if(gameStart){
guiNode = app.getGuiNode();
rootNode = app.getRootNode();
flyCam = app.getFlyByCamera();
flyCam.setEnabled(true);
flyCam.setDragToRotate(false);
base.gameStartSettings();
gameStart = false;
gameUpdate = true;
}
if(gameUpdate){
base.onGameUpdate();;
}

}
}[/java]

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) gBase.cam.getDirection(). 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]
java.lang.NullPointerException
at gamerScreen.GuiGameBase.onGameUpdate(GuiGameBase.java:287)
at gamerScreen.HudScreen.update(HudScreen.java:164)
at com.jme3.app.state.AppStateManager.update(AppStateManager.java:249)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:233)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:182)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)
at java.lang.Thread.run(Thread.java:662)[/xml]

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.