[SOLVED] Java nullpointer error

Hi everybody my name’s Brian. I’m working in a book called JMonkey Engine 3.0 and it’s really good I’ve been able to resolve all my issues until now. I’m working on button mapping for my game and to learn the fundamental concepts but I keep getting this error(posted below) along with the actual code of the program. It compiles fine but then I get a runtime error when I use the button I made, can anybody help me with this?

here is the popup error
Uncaught exception thrown in Thread[jME3 Main,5,main]
NullPointerException: Cannot invoke “com.jme3.scene.Geometry.getMaterial()” because “this.this$0.geom” is null

Here is what it prints to the console after closing the popup and returning to the engine
java.lang.NullPointerException: Cannot invoke “com.jme3.scene.Geometry.getMaterial()” because “this.this$0.geom” is null
at mygame.Main$1.onAction(Main.java:118)
at com.jme3.input.InputManager.invokeActions(InputManager.java:174)
at com.jme3.input.InputManager.onKeyEventQueued(InputManager.java:474)
at com.jme3.input.InputManager.processQueue(InputManager.java:871)
at com.jme3.input.InputManager.update(InputManager.java:923)
at com.jme3.app.LegacyApplication.update(LegacyApplication.java:785)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:248)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:160)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:225)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:242)
at java.base/java.lang.Thread.run(Thread.java:840)

and here is the actual code of the game I wrote

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.math.FastMath;
import com.jme3.system.AppSettings;
import com.jme3.input.*;
import com.jme3.input.controls.*;

/**
 * This is the Main Class of your Game. You should only do initialization here.
 * Move your Logic into AppStates or Controls
 * @author normenhansen
 */
public class Main extends SimpleApplication {
    
    //geometry mesh instance
         private Geometry geom2;
         private Geometry geom; 
     
     
    //class objects for keys and buttons
    private final static Trigger TRIGGER_COLOR = new KeyTrigger(KeyInput.KEY_SPACE);
    private final static Trigger TRIGGER_ROTATE = new MouseButtonTrigger(MouseInput.BUTTON_LEFT);
    private final static String MAPPING_COLOR = "Toggle color";
    private final static String MAPPING_ROTATE = "Rotate";

    public static void main(String[] args) {
        
        
        //app object
        Main app = new Main();
        
        
        //settings
        //app settings object
        AppSettings settings = new AppSettings(true);
        //resolution settings
        settings.setResolution(450, 640);
        //splashscreen image
        settings.setSettingsDialogImage("Interface/splashscreen.jpg");
        //game title
        settings.setTitle("Laser Cube");
        //apply settings
        app.setSettings(settings);
        app.start();
        
        
        
    }

    @Override
    public void simpleInitApp() {
        //addMaps and addListeners for keys and buttons
        inputManager.addMapping(MAPPING_COLOR, TRIGGER_COLOR);
        inputManager.addMapping(MAPPING_ROTATE, TRIGGER_ROTATE);
        inputManager.addListener(actionListener, new String[]{MAPPING_COLOR});
        inputManager.addListener(analogListener, new String[]{MAPPING_ROTATE});
        
       
        
        //first original box
        Box b = new Box(Vector3f.ZERO, 1, 1, 1);
        Geometry geom = new Geometry("Box", b);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        geom.setMaterial(mat);
        rootNode.attachChild(geom);
        //shrink the blue cube
        geom.setLocalScale(0.5f);
        //rotate the cube with quaternions
        Quaternion roll045 = new Quaternion();
        //supply angel and axis as arguments
        roll045.fromAngleAxis(45*FastMath.DEG_TO_RAD, Vector3f.UNIT_X);
        geom.setLocalRotation(roll045);
        
         
        
        //my box
        //create vecot object
        Vector3f v2 = new Vector3f(2.0f, 1.0f, -3.0f);
        //create box object
         Box b2 = new Box(Vector3f.ZERO, 1, 1, 1);
        //create geometry for box object
        
        Geometry geom2 = new Geometry("Box", b2);
        //create materal for box object
        Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        //assign color to material
        mat2.setColor("Color", ColorRGBA.Yellow);
        //set material to geometry
        geom2.setMaterial(mat2);
        //set location of cube
        geom2.setLocalTranslation(v2);
        //attach geometry to root node
        rootNode.attachChild(geom2);
        //enlarge the yellow cube
        geom2.scale(2.0f);
        //rotate the cube by degrees to radians
        float r = FastMath.DEG_TO_RAD * 45;
        geom2.rotate(0.0f, r, 0.0f);
    }
    //listener objects for keys and buttons
    private ActionListener actionListener = new ActionListener()
            {
            public void onAction(String name, boolean isPressed, float tpf)
            {
            System.out.println("You triggered: " + name);
if(name.equals(MAPPING_COLOR) && !isPressed)
{
//implement action here -- change cubes color
geom.getMaterial().setColor("Color", ColorRGBA.randomColor());
}
            }
            };
    private AnalogListener analogListener = new AnalogListener()
            {
            public void onAnalog(String name, float intensity, float tpf)
            {
            System.out.println("You triggered: " + name);
if(name.equals(MAPPING_ROTATE))
{
//implement action here -- rotate cube
geom2.rotate(0, intensity, 0);//rotate around y-axis
}

            }
            };

    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }
}

Thank you all very much for your help and I hope we can resolve this pretty quick.

It looks like you are encountering a “local variable hides a field” issue related to a double declaration of your variable named “geom”

You can probably google that term I put quotes for a more detailed explanation than I can give.

But to summarize: the class variable that you named “geom” is staying null because you also named another local variable as “geom” in your simpleInitApp() app method. So when you are assigning a value to “geom” it is only being assigned to the local variable, leaving your class variable null.

So the easy solution is to change this line of code:

to instead be this:

        //first original box
         Box b = new Box(Vector3f.ZERO, 1, 1, 1);
         geom = new Geometry("Box", b);
4 Likes

Wow, incredible and that’s amazing I am so happy you responded so quickly. You solved thanks a million I appreciate that!

3 Likes