Control causes NullPointerException

I have created a control for player movement but for some reason it causes a Null Pointer Exception when I do any of these three things.

  1. moveControl.registerWithInput(inputManager);



    public void registerWithInput(InputManager inputManager){

    this.inputManager = inputManager;

    inputManager.addMapping("Player_Left", new MouseAxisTrigger(MouseInput.AXIS_X, true),

    new KeyTrigger(KeyInput.KEY_LEFT));

    inputManager.addMapping("Player_Right", new MouseAxisTrigger(MouseInput.AXIS_X, false),

    new KeyTrigger(KeyInput.KEY_RIGHT));

    inputManager.addMapping("Player_Up", new MouseAxisTrigger(MouseInput.AXIS_Y, false),

    new KeyTrigger(KeyInput.KEY_UP));

    inputManager.addMapping("Player_Down", new MouseAxisTrigger(MouseInput.AXIS_Y, true),

    new KeyTrigger(KeyInput.KEY_DOWN));

    inputManager.addMapping("Player_Forward", new KeyTrigger(KeyInput.KEY_W));

    inputManager.addMapping("Player_Backward", new KeyTrigger(KeyInput.KEY_S));

    inputManager.addMapping("Player_StrafeLeft", new KeyTrigger(KeyInput.KEY_A));

    inputManager.addMapping("Player_StrafeRight", new KeyTrigger(KeyInput.KEY_D));


    inputManager.addMapping("Player_Rise", new KeyTrigger(KeyInput.KEY_Q));

    inputManager.addMapping("Player_Lower", new KeyTrigger(KeyInput.KEY_Z));


    inputManager.addListener(this, mappings);

    inputManager.setCursorVisible(dragToRotate || !isEnabled());

    Joystick[] joysticks = inputManager.getJoysticks();

    if (joysticks != null && joysticks.length > 0){

    Joystick joystick = joysticks[0];

    joystick.assignAxis("Player_StrafeRight", "Player_StrafeLeft", JoyInput.AXIS_POV_X);

    joystick.assignAxis("Player_Forward", "Player_Backward", JoyInput.AXIS_POV_Y);

    joystick.assignAxis("Player_Right", "Player_Left", joystick.getXAxisIndex());

    joystick.assignAxis("Player_Down", "Player_Up", joystick.getYAxisIndex());




    calls the standard method in spatial.





    public void setSpatial(Spatial spatial) {



    Any help fixing the NPE would be great thanks.

generally speaking:

rotationSpeed, value or axis is null (mat cannot be, I can see that)

if you right-click the “Matrix3f” class in the editor and select “show source” you can find out that on line 742 the following happens:

[java] float fX2 = axis.x * axis.x;


…which leads us to the conclusion that axis is null in this case.

Edit: … and actually, the exception would have happened in that line if rotationSpeed or value had been null, so you don’t even need to look into the Matrix3f source… But its helpful anyway :wink:

posting the stack trace would be helpful…

inputManager.addListener(this, mappings); → do you ever fill that array?

Sorry to ask this but how do you print a stack trace in the JMP I can’t find how to do it?


:? Its printed there where you read about the NPE in the first place?

Sorry did not realize that was called the stack trace. I have never been asked for it before probably because I almost always provide it in my origional post. :slight_smile:

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]


at GameCore.BasicGame.simpleInitApp(


at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(



Thanks normen

It happens in your application class in line 84, one of the objects you try to use is null, find out which and then why.

I figured out that issue I forgot to initialize the control to it was just a reference. Now I am back to the null pointer exception that I was fighting before I switched the game controls into a control class.


SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]


at com.jme3.math.Matrix3f.fromAngleNormalAxis(

at GameCore.PlayerMoveControl.rotatePlayer(

at GameCore.PlayerMoveControl.onAnalog(

at com.jme3.input.InputManager.invokeAnalogsAndActions(

at com.jme3.input.InputManager.onMouseMotionEventQueued(

at com.jme3.input.InputManager.processQueue(

at com.jme3.input.InputManager.update(



at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(




Line 214 in PlayerMoveControl is line 2 in here:

[java] Matrix3f mat = new Matrix3f();

mat.fromAngleNormalAxis(rotationSpeed * value, axis);

Vector3f up = getUp();

Vector3f left = getLeft();

Vector3f dir = getDirection();

mat.mult(up, up);

mat.mult(left, left);

mat.mult(dir, dir);

Quaternion q = new Quaternion();

q.fromAxes(left, up, dir);




The other error is caused by the line corresponding to the direction that the mouse is moved first.


if (name.equals("Player_Left")){

rotatePlayer(value, initialUpVec); //this line throws the error if the mouse is moved left first

}else if (name.equals("Player_Right")){

rotatePlayer(-value, initialUpVec); //ect.

}else if (name.equals("Player_Up")){

rotatePlayer(-value, spatial.getLocalTranslation()); //ect.

}else if (name.equals("Player_Down")){

rotatePlayer(value, getLeft()); //ect.



I can’t seem to find where the exception is coming from I have looked into value and rotationSpeed but it appears to be impossible for either to be null. The rotation speed is a constant and value is controlled by the engine and as far as I can tell will never be null.


Edit: found the problem.

This NPE was caused by another null object, the initialUpVector, I have moved this code around so much that I lost a declaration. :slight_smile:

Thanks for the help normen.

Like I said, axis was null from the information you gave.