Search code examples
javanullpointerexceptionjmonkeyengine

NullPointerException that wont go away


So for those of you that are familiar with the jMonkey Engine, I have this code:

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

This code used to be working until I added the

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

part. The code that I added was working in a different test file in another project but now it has stopped working here. It MUST be in the simpleUpdate() loop but I don't see why it is getting this 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)

If someone knows why this is happening please help me! My only solution is I might need to create another class file. Thanks in advance!

EDIT: This is the line that is being called null:

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

EDIT 2: Here is where I initialize it:

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

EDIT 3 (if anyone is still here): I have figured out by doing

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

that cubesSettings.getBlockSize() is a float as well as tpf being a float. What could be null?!?


Solution

  • I figured it out! I just needed to initialize the cubesSettings variable withing the update method again! The nullPointerException is gone! I am left with a new more confusing one but it does not pertain to this question and is material for another question. Thank you all!