2

所以这个问题适合那些熟悉 jMonkey 3 引擎的人。我的simpleUpdate()循环中有这段代码:

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

我留下了这个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)

第 219 行是这一行:

playerControl.setWalkDirection(walkDirection);

我已经检查过了,playerControl并且walkDirection两者都不为空。我的问题是:还有什么可能是空的?如何修复此错误?为什么会这样?我如何防止这种情况在未来发生?对此问题的任何帮助将不胜感激,如果您需要查看完整代码,请询问!我非常乐意与您分享!提前致谢!

4

1 回答 1

1

如果您正在实例化CharacterControl无参数,则不会初始化类character中的成员。PhysicsCharacter

完整的跟踪将是:

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

当您调用方法时,这就是问题所在playerControl.setWalkDirection

所以你必须使用另一个构造函数,比如CharacterControl(CollisionShape shape, float stepHeight).

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

有关参数值和其他内容,请参见:

于 2014-03-20T15:16:06.960 回答