0

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?!?

1Poseidon3
  • 125
  • 1
  • 11
  • 3
    you got everything you need... have a look at: at com.bminus.Main.simpleUpdate(Main.java:171) main.java at line 171... what you can find there? – Danny. Mar 14 '14 at 15:07
  • 1
    Where is line 171 of Main.java in the sample? – Jonathan Drapeau Mar 14 '14 at 15:08
  • 2
    It seems you are young, so welcome to progammer community. Put a breakpoint on the line 171, start the program in debug, and check out the variable values. Which is null from those variables what are used in line 171? – CsBalazsHungary Mar 14 '14 at 15:33
  • @Dan P @ Jonathan Drapeau Yes I am somewhat young (not an adult) and thanks for welcomeing me :P The null appears to be on the line with the float which makes me thing that on of the variable it is using might be null? – 1Poseidon3 Mar 14 '14 at 16:26
  • Oh well, everybody starts somewhere :) the problem is usually something like: myObject.doSomething() when myObject didn't have value, so it is null, and you can't do anything with null except checking it if it is equal to something else. So in your case cubesSettings is null most probably, check it with debug. – CsBalazsHungary Mar 14 '14 at 20:33
  • 1
    Where is `cubesSettings` initialized? I don't see any code initializing it, just using it. – SergeyB Mar 14 '14 at 20:56

2 Answers2

0

Not really an answer but something you should do when you run into null pointer.

  1. You can log everthing thats happening in the program
  2. You can just simply do System.out.println(variablename);

From what you have given us, its hard to justify exactly why / of from where.. you have null pointer.

So what you could do before this is called

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

is simply put this..

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

And check in console which value is null ;)

Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72
  • either `cubeSettings` is null or `cubesSettings.getBlockSize()` is null (which can only be if it returns a wrapped primitive like `Float`(with capital F). `tpf` is a primitive value and cannot be null. My guess is that `cubesSettings` is null. – mihi Mar 14 '14 at 20:15
  • @mihi what if he sets tpf to null somewhere ? without even knowing this ? we do not have full code – Maciej Cygan Mar 15 '14 at 12:56
  • tpf is declared as `float` (it is a parameter to the function), and you cannot set a primitive type like `float` to `null` (because `null` is a reference that references nothing, and primitive types are not reference types) – mihi Mar 15 '14 at 15:54
  • Trying this. Sorry T haven't been checking this in a while. I got really caught up in other things. – 1Poseidon3 Mar 18 '14 at 14:28
  • It appears the `cubesSettings.getBlockSize();` is what is returning null. I will look into why this is returning null as the cubesSettings stuff refers to a framework I am using. Chances are I just didn't initialize it. – 1Poseidon3 Mar 18 '14 at 14:31
  • The weird part is that this `cubeSettings.getBlockSize();` is called in other places in the code and works just fine there. I can display more code if needed. @mihi – 1Poseidon3 Mar 18 '14 at 14:39
  • @1Poseidon3 Can you show code where you initialize (assign to) `cubesSettings` variable? Probably you are intializing it differently if it sometimes has a block size and sometimes it does not. – mihi Mar 18 '14 at 18:18
  • @mihi I fixed it. I just took the non-working code, put in another method where `cubesSettings.getBlockSize();` was working and the called that method in the update method. I am leftwith another problem now but not pertaining to this. Thanks for all of your help though guys! :D – 1Poseidon3 Mar 19 '14 at 20:35
  • SCRATCH ALL OF THAT! I figured out that just `cubesSetings` is null for some reason. I don't why it is doing this though! D: – 1Poseidon3 Mar 20 '14 at 14:25
0

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!

1Poseidon3
  • 125
  • 1
  • 11