Noob question:java.lang.NullPointerException

Hi everyone,

As I wrote above, I a little experience in Java and still total noob in jME3. So what I am trying t do is a simple application, just to show a Cube. I created two classes Main and Mybox:

[java]

public class Main extends SimpleApplication {

public static void main(String[] args) {

Main app = new Main();

app.start();

}

@Override

public void simpleInitApp() {

MyBox mb = new MyBox();

mb.Start();

}

}

[/java]

[java]

package mygame;

import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.math.ColorRGBA;

import com.jme3.scene.shape.Box;

public class MyBox extends SimpleApplication {

@Override

public void simpleInitApp() {

Box c = new Box(Vector3f.ZERO, 1, 1, 1);

Geometry geom1 = new Geometry("Box", c);

Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

mat1.setColor("Color", ColorRGBA.Blue);

geom1.setMaterial(mat1);

rootNode.attachChild(geom1);

}

public void Start(){

simpleInitApp();

}

}

[/java]

In class Main I try to call Instance of MyBox but I get the following exception:

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

at com.jme3.material.Material.(Material.java:167)

at mygame.MyBox.simpleInitApp(MyBox.java:25)

at mygame.MyBox.Start(MyBox.java:32)

at mygame.Main.simpleInitApp(Main.java:93)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:218)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:138)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:206)

at java.lang.Thread.run(Thread.java:619)

I think it must be because my lack of knowledge of Java, but I need to know where to search the answer.

Thanks,

assetManager is null when you create mat1 in the MyBox class.

the SimpleApplicaiton class has several attributes (including the assetmanager) that are initialized in the start() method.

the problem is that you are misusing the SimpleApplication class.

MyBox shouldn’t be a SimpleApplication, because you’ll never call the start() method.

Also you’re gonna have the exact same issue with the rootNode.

you have 2 alternatives :

  • get rid of the MyBox class and do your simpleInitApp code in the Main class
  • Make MyBox not extend anything, and just pass to it the Main class asset manager in it’s constructor so you can use it later.

    like this :

    [java]

    public class Main extends SimpleApplication {

    public static void main(String[] args) {

    Main app = new Main();

    app.start();

    }

    @Override

    public void simpleInitApp() {

    MyBox mb = new MyBox( assetManager, rootNode);

    mb.init();

    }

    }

    public class MyBox {

    private AssetManager assetManager;

    private Node rootNode

    public MyBox(AssetManager assetManager, Node rootNode) {

    this.assetManager=assetManager;

    this.rootNode=rootNode;

    }

    public void init(){

    Box c = new Box(Vector3f.ZERO, 1, 1, 1);

    Geometry geom1 = new Geometry(“Box”, c);

    Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

    mat1.setColor(“Color”, ColorRGBA.Blue);

    geom1.setMaterial(mat1);

    rootNode.attachChild(geom1);

    }

    }

    [/java]
1 Like

Try the tutorials. They show you how to create a box…

RE:nehon

Thanks a lot, this is exactly what I want to know. Your advise very helpful.

Re: nehon

Thank you brother you saved me from going insane trying to separate all my work in various classes. I’m a 3D artist not really a programmer but little by little i’m getting there. Thanks again.