Null Pointer exception when loading Unshaded.j3md

I’m getting this error when I try to load a material from my obstacle class:



[java]java.lang.NullPointerException

at com.jme3.material.Material.<init>(Material.java:167)

at pawn.Obstacle.Create(Obstacle.java:31)

at pawn.Main.createObstacles(Main.java:79)

at pawn.Main.simpleInitApp(Main.java:32)

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)[/java]



obstacle create looks like this:



[java]public Geometry Create (){

Box body = new Box(new Vector3f(1,2,1), 0.5f, 0.5f, 0.5f);

Geometry geom = new Geometry (“Body”, body);

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

Texture tex = assetManager.loadTexture(“Textures/sq_face_green.jpg”);



mat.setTexture(“Color Map”, tex);

geom.setMaterial(mat);



return geom;

} [/java]



how would I fix this? thanks in advance!

Probably the texture is null, check the file case and name.

I just checked the filename and file path, everything seems to be in order there, the name is copied directly from the file itself.

can you check if “tex” is null after you load it? I am almost 100% positive that must be it.

hmm, the funny thing is the tex line is never even run, the game crashes on the [java] Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");[/java]

Are you working with OSGi? Or with threads?

nope nothing complicated like that, infact I was able to load these textures before when I was loading (via a method) them in the main class, but that proved to be ineffective since I wanted obstacles to have variations and different properties so that called for classes. I don’t know if it helps but here is the entire obstacle class:



[java]/*

  • To change this template, choose Tools | Templates and open the template in
  • the editor.

    */

    package pawn;



    import com.jme3.material.Material;

    import com.jme3.math.Vector3f;

    import com.jme3.scene.Geometry;

    import com.jme3.scene.shape.Box;

    import com.jme3.texture.Texture;

    import com.jme3.asset.AssetManager;





    /**

    *
  • @author Alex

    */

    public class Obstacle {



    protected int Id = 0;

    protected String Name = "Normal Block";

    protected int health = 100;

    protected int attack = 0;



    private AssetManager assetManager;



    public Geometry Create (){

    Box body = new Box(new Vector3f(1,2,1), 0.5f, 0.5f, 0.5f);

    Geometry geom = new Geometry ("Body", body);

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

    Texture tex = assetManager.loadTexture("Textures/sq_face_green.jpg");



    mat.setTexture("Color Map", tex);

    geom.setMaterial(mat);



    return geom;

    }



    public void Take_Damage (int hit){



    }



    public int getAttack (){

    return attack;

    }



    }

    [/java]



    and here is the create obstacles method:



    [java]public void createObstacles(int width, int length){



    Random r = new Random();



    for(int q = 0; q < length; q++)

    for(int i = 0; i < width; i ++){

    if (r.nextInt(3)== 0){

    int id = r.nextInt(1);

    if ( id == 0){

    Obstacle ob = new Obstacle();

    rootNode.attachChild(ob.Create());

    }

    if (id == 1){

    HeavyBlock ob = new HeavyBlock();

    rootNode.attachChild(ob.Create());

    }

    }

    };

    }[/java]



    This isnt the best design I know but right now im just playing around with a few things.

My guess: assetManager is null. I don’t see anywhere above that it gets set.

1 Like

Sorry for the extreme nooby question Im about to ask but what should I set asset manager to?

The asset manager that you have in your application class. Variables that aren’t set to anything don’t have any values.



Writing 3D games is one of the hardest types of programming to do… and that’s for people who already know the language. You should consider investing some more time in learning Java without the added burden of trying to learn 3D games. If you have the patience to do that then the rest of your coding life will go 1000 times easier.

http://www.javabeginner.com/

Well, Im actually taking a java course right now :slight_smile: We are almost done and really Im doing this just to play around and see where I can go with it not too worried about a full fledged game. Thanks for the help though!

2 Likes

:D. You’ve understood the spirit of the thing. The last one we gave him that link, was very embarrassed http://hub.jmonkeyengine.org/groups/jmonkeyplatform/forum/topic/rootnode-inside-of-class.