NullPointerException不会消失

时间:2014-03-14 15:05:57

标签: java nullpointerexception jmonkeyengine

所以对于那些熟悉jMonkey Engine的人,我有这个代码:

@Override
    public void simpleUpdate(float tpf) {
        if (load) {
            if (frameCount == 1) {
                Element element = nifty.getScreen("loadlevel").findElementByName("loadingtext");
                textRenderer = element.getRenderer(TextRenderer.class);
                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);

                setProgress(1.0f, "Done");
            } else if (frameCount == 6) {
                nifty.gotoScreen("end");
                nifty.exit();
                guiViewPort.removeProcessor(niftyDisplay);
            }
            frameCount++;
        }

        float playerMoveSpeed = ((cubesSettings.getBlockSize() * 2.5f) * tpf);
        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.setY(0);
        playerControl.setWalkDirection(walkDirection);
        cam.setLocation(playerControl.getPhysicsLocation());
    }

此代码在我添加

之前一直有效
float playerMoveSpeed = ((cubesSettings.getBlockSize() * 2.5f) * tpf);
        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.setY(0);
        playerControl.setWalkDirection(walkDirection);
        cam.setLocation(playerControl.getPhysicsLocation());

一部分。我添加的代码是在另一个项目的另一个测试文件中工作,但现在它已停止在这里工作。它必须在simpleUpdate()循环中,但我不明白为什么它会得到这个NullPointerException:

java.lang.NullPointerException
    at com.bminus.Main.simpleUpdate(Main.java:171)
    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)

如果有人知道为什么会这样,请帮助我!我唯一的解决方案是我可能需要创建另一个类文件。提前谢谢!

编辑:这是被称为null的行:

float playerMoveSpeed = ((cubesSettings.getBlockSize() * 2.5f) * tpf);

编辑2:我在这里初始化它:

public class Main extends SimpleApplication implements ScreenController, Controller, ActionListener {

private static final Logger logger = Logger.getLogger(Main.class.getName());
private NiftyJmeDisplay niftyDisplay;
private Nifty nifty;
private Element progressBarElement;
private float frameCount = 0;
private boolean load = false;
private TextRenderer textRenderer;
private final Vector3Int terrainSize = new Vector3Int(100, 30, 100);
private BulletAppState bulletAppState;
private CharacterControl playerControl;
private Vector3f walkDirection = new Vector3f();
private boolean[] arrowKeys = new boolean[4];
private CubesSettings cubesSettings;
private BlockTerrainControl blockTerrain;
private Node terrainNode = new Node();

编辑3(如果有人还在这里):我已经弄明白了

if (cubesSettings.getBlockSize() == null) {
    logger.log(Level.SEVERE, "null");
}

cubesSettings.getBlockSize()是浮点数,tpf是浮点数。什么可能是空的?!?

2 个答案:

答案 0 :(得分:0)

不是一个真正的答案,而是当你遇到空指针时应该做的事情。

  1. 您可以记录程序中发生的情况
  2. 您只需执行System.out.println(variablename);
  3. 即可

    从你给我们的内容来看,很难证明为什么/从哪里来......你有空指针。

    所以在此之前你可以做什么叫做

    float playerMoveSpeed = ((cubesSettings.getBlockSize() * 2.5f) * tpf);
    

    简单地说就是这个..

    System.out.println(cubeSettings.getBlockSize());
    System.out.println(tpf);
    

    并在控制台中检查哪个值为null;)

答案 1 :(得分:0)

我明白了!我只需要再次使用update方法初始化cubesSettings变量! nullPointerException消失了!我留下了一个更令人困惑的新问题,但它不属于这个问题而且是另一个问题的材料。谢谢大家!