Question

So this question is for those of you who are familiar with the jMonkey 3 Engine. I have this code withing my simpleUpdate() loop:

@Override
    public void simpleUpdate(float lastTimePerFrame) {
        if (load) {
            if (frameCount == 1) {
                Element element = nifty.getScreen("loadlevel").findElementByName("loadingtext");
                textRenderer = element.getRenderer(TextRenderer.class);
                inputManager.setCursorVisible(true);
                flyCam.setDragToRotate(true);
                CubesTestAssets.registerBlocks();

                setProgress(0.2f, "Registering Blocks");
            } else if (frameCount == 2) {
                initBlockTerrain();

                setProgress(0.4f, "Generating Chunk");
            } else if (frameCount == 3) {
                initControls();
                initPlayer();

                setProgress(0.6f, "Setting Up Player");
            } else if (frameCount == 4) {
                viewPort.setBackgroundColor(ColorRGBA.Cyan);

                setProgress(0.8f, "Creating Sky");
            } else if (frameCount == 5) {
                inputManager.setCursorVisible(false);
                flyCam.setDragToRotate(false);

                setProgress(1.0f, "Done");
            } else if (frameCount == 6) {
                nifty.gotoScreen("end");
                nifty.exit();
                guiViewPort.removeProcessor(niftyDisplay);
            }
            frameCount++;
        }
        cubesSettings = CubesTestAssets.getSettings(this);
        playerControl = new CharacterControl();
        float playerMoveSpeed = ((cubesSettings.getBlockSize() * 2.5f) * lastTimePerFrame);
        Vector3f camDir = cam.getDirection().mult(playerMoveSpeed);
        Vector3f camLeft = cam.getLeft().mult(playerMoveSpeed);
        walkDirection.set(0, 0, 0);
        if(arrowKeys[0]){ walkDirection.addLocal(camDir); }
        if(arrowKeys[1]){ walkDirection.addLocal(camLeft.negate()); }
        if(arrowKeys[2]){ walkDirection.addLocal(camDir.negate()); }
        if(arrowKeys[3]){ walkDirection.addLocal(camLeft); }
        walkDirection.set(0, 0, 0);
        walkDirection.setY(0);
        playerControl.setWalkDirection(walkDirection);
        cam.setLocation(playerControl.getPhysicsLocation());
    }

and I am left with this nullPointerException:

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
    at com.jme3.bullet.objects.PhysicsCharacter.setWalkDirection(PhysicsCharacter.java:115)
    at com.bminus.Main.simpleUpdate(Main.java:219)
    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:744)

Line 219 is this line:

playerControl.setWalkDirection(walkDirection);

I have already checked and playerControl and walkDirection both are not null. My question is: What else could be null? How do I fix this error? Why is this happening? How do I prevent this from happening in the future? any help on this problem would be greatly appreciated and if you need to see the full code, just ask! I would be more than glad to share it with you! Thanks in advance!

Was it helpful?

Solution

If you're instantiating your CharacterControl without parameter, the character member in the PhysicsCharacter class won't be initialized.

The full trace would be:

=>  simpleUpdate()
    =>  playerControl.setWalkDirection()
        =>  character.setWalkDirection(Converter.convert(walkDirection, tempVec));
            => NullPointerException on character object

This is the issue, when you're calling the method playerControl.setWalkDirection.

So you have to use another constructor like CharacterControl(CollisionShape shape, float stepHeight).

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

For parameter values and other stuff see:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top