【问题标题】:NullPointerException that wont go away不会消失的 NullPointerException
【发布时间】:2014-03-14 15:05:57
【问题描述】:

所以对于那些熟悉 jMonkey 引擎的人来说,我有这个代码:

@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 是一个浮点数。什么可能是空的?!?

【问题讨论】:

  • 你得到了你需要的一切......看看:在第 171 行的 com.bminus.Main.simpleUpdate(Main.java:171) main.java......你可以在那里找到什么?
  • 示例中 Main.java 的第 171 行在哪里?
  • 看来你还年轻,欢迎加入职业玩家社区。在第 171 行下一个断点,在调试中启动程序,并检查变量值。在第 171 行中使用的那些变量中,哪个为空?
  • @Dan P @ Jonathan Drapeau 是的,我有点年轻(不是成年人),感谢您对我的欢迎:P null 似乎与浮动在线,这让我觉得它使用的变量可能为空?
  • cubesSettings 在哪里初始化?我没有看到任何代码初始化它,只是使用它。

标签: java nullpointerexception jmonkeyengine


【解决方案1】:

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

  1. 您可以记录程序中发生的一切
  2. 你可以简单地做System.out.println(variablename);

从你给我们的信息来看,很难准确地证明为什么 / of from where.. 你有空指针。

所以在这之前你可以做的事情叫做

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

简单来说就是这样。

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

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

【讨论】:

  • cubeSettings 为 null 或 cubesSettings.getBlockSize() 为 null(仅当它返回像 Float(大写 F)这样的包装原语时才可以。tpf 是原语值,不能为空。我猜cubesSettings 为空。
  • @mihi 如果他在某处将 tpf 设置为 null 怎么办?甚至不知道这一点?我们没有完整的代码
  • tpf 被声明为float(它是函数的参数),并且您不能将float 之类的原始类型设置为null(因为null 是引用的引用什么都没有,原始类型不是引用类型)
  • 试试这个。对不起,T 有一段时间没有检查这个了。我真的很忙于其他事情。
  • 看来 cubesSettings.getBlockSize(); 是返回 null 的内容。我将研究为什么这会返回 null,因为 cubesSettings 的东西是指我正在使用的框架。可能是我没有初始化它。
【解决方案2】:

我想通了!我只需要再次使用更新方法初始化cubesSettings 变量! nullPointerException 消失了!我留下了一个新的更令人困惑的问题,但它与这个问题无关,并且对另一个问题很重要。谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多