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);

    calls:

    [java]

    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());

    }

    }[/java]



    2)playerNode.addControl(moveControl);

    calls the standard method in spatial.



    3)moveControl.setSpatial(playerNode);

    calls:

    [java]

    @Override

    public void setSpatial(Spatial spatial) {

    super.setSpatial(spatial);

    }[/java]



    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;

[/java]

…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?



Thanks

:? 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]

java.lang.NullPointerException

at GameCore.BasicGame.simpleInitApp(BasicGame.java:84)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:228)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)

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

at java.lang.Thread.run(Thread.java:722)



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.



[java]

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

java.lang.NullPointerException

at com.jme3.math.Matrix3f.fromAngleNormalAxis(Matrix3f.java:742)

at GameCore.PlayerMoveControl.rotatePlayer(PlayerMoveControl.java:214)

at GameCore.PlayerMoveControl.onAnalog(PlayerMoveControl.java:150)

at com.jme3.input.InputManager.invokeAnalogsAndActions(InputManager.java:279)

at com.jme3.input.InputManager.onMouseMotionEventQueued(InputManager.java:377)

at com.jme3.input.InputManager.processQueue(InputManager.java:797)

at com.jme3.input.InputManager.update(InputManager.java:851)

at com.jme3.app.Application.update(Application.java:598)

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:722)a

[/java]



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);

q.normalizeLocal();



setAxes(q);

[/java]



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

[java]



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.



[/java]



Thanks.

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.



Thanks



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.