[SOLVED] Problem loading using assetManager

Hi I’m new looking for some help loading files.
I have set up my first default project but I get a Null Point exception every time I try to load any files like this.

   public Entities(AssetManager assMan, Node rNode){
    this.pos = new Vector3f(0.0f, 0.0f, 0.0f);
    this.scale = new Vector3f(1.0f, 1.0f, 1.0f);
    this.name = "";
    this.health = 100;
    this.defaultbox = new Box(1,1,1);
    
    this.playerdef = new Geometry("player", defaultbox);
    this.material = new Material(assMan, "assets/Materials/test.j3m");
    this.material.setColor("colour", ColorRGBA.Blue);
    this.playerdef.setMaterial(material);
    rNode.attachChild(playerdef);

and main

   public class Main extends SimpleApplication {

public AssetManager assMan = this.getAssetManager();
public Node rNode = this.getRootNode();


public static void main(String[] args) {
    Main app = new Main();
    app.start();
}

@Override
public void simpleInitApp() {
  
    Vector3f position = new Vector3f(1.0f, 1.0f, 1.0f);        
    Player masterchief1 = new Player(this.assMan, this.rNode);
   
}

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

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

}

You cannot do that. At the time that line gets invoked - which is before the constructor - it is null. The variable assetManager already exists in a class that extends SimpleApplication.

Change this line:

Player masterchief1 = new Player(this.assMan, this.rNode);

to

Player masterchief1 = new Player(assetManager, this.rNode);

As people already stated on discord: just saying you get a null pointer exception without posting the stack trace sadly isn’t enough for us to be able to help.

Without the stack trace I can only guess, but if the exception happens on the masterchief line, we will very likely also need your Player class to be able to help.

EDIT: nevermind, Jay was faster. That said, you might still want to remember that stack traces are very important :wink:

Then how do I pass my Entity class AssetManager? I’m not extending simpleClass in Entity class

here’s the stack tace

java.lang.NullPointerException
at com.jme3.material.Material.checkSetParam(Material.java:457)
at com.jme3.material.Material.setParam(Material.java:475)
at com.jme3.material.Material.setColor(Material.java:660)
at mygame.Entities.(Entities.java:42)
at mygame.Player.(Player.java:19)
at mygame.Main.simpleInitApp(Main.java:30)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:220)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:211)
at java.lang.Thread.run(Thread.java:748)

Please read my reply again - I updated it.

Thanks for the help appreciate it.

quick follow up how do I copy file paths in the JDK as now I’m getting an asset not found exception.

this is my project it’s just the default.
139

To reference that test.j3m you would specify:

assetManager.loadMaterial(assetManager, "Materials/test.j3m");

This is because you can register paths in the assetManager. The “Project Assets” directory is already registered for you, so you only reference folders and items from within the registered path(s).

Edit - updated the code. You use assetManager.loadMaterial("path"); for j3m files.

Thanks I believe that has worled but I’m Still getting an exception I just pulled one of the default textures out of the test library not sure if there’s a problem with the material.

SEVERE: Uncaught exception thrown in Thread[jME3 Main,5,main]
com.jme3.asset.AssetLoadException: An exception has occurred while loading asset: Materials/test.j3m
at com.jme3.asset.DesktopAssetManager.loadLocatedAsset(DesktopAssetManager.java:261)
at com.jme3.asset.DesktopAssetManager.loadAsset(DesktopAssetManager.java:373)
at com.jme3.material.Material.(Material.java:107)
at mygame.Entities.(Entities.java:41)
at mygame.Player.(Player.java:19)
at mygame.Main.simpleInitApp(Main.java:30)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:220)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:211)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: Material instances must be loaded via MaterialKey
at com.jme3.material.plugins.J3MLoader.load(J3MLoader.java:791)
at com.jme3.asset.DesktopAssetManager.loadLocatedAsset(DesktopAssetManager.java:259)
… 9 more

Again - please re-read my reply.

Sorry didn’t realise you had edited it. That works but now it’s saying I have not defined a colour even though I set it in the next line

java.lang.IllegalArgumentException: Material parameter is not defined: colour
at com.jme3.material.Material.checkSetParam(Material.java:459)
at com.jme3.material.Material.setParam(Material.java:475)
at com.jme3.material.Material.setColor(Material.java:660)
at mygame.Entities.(Entities.java:42)
at mygame.Player.(Player.java:19)
at mygame.Main.simpleInitApp(Main.java:30)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:220)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:211)
at java.lang.Thread.run(Thread.java:748)

Properties are case sensitive. Use Color and not color.

I am using Color

I believe that’s just the name I passed it as a String

From the code I can see, your’re using colour.

Still the same problem

java.lang.IllegalArgumentException: Material parameter is not defined: Color
at com.jme3.material.Material.checkSetParam(Material.java:459)
at com.jme3.material.Material.setParam(Material.java:475)
at com.jme3.material.Material.setColor(Material.java:660)
at mygame.Entities.(Entities.java:42)
at mygame.Player.(Player.java:19)
at mygame.Main.simpleInitApp(Main.java:30)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:220)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:211)
at java.lang.Thread.run(Thread.java:748)

It’s simply saying that there is no property called “Color”. I’m not sure what you’re trying to do - and I’m not sure you are either. You’ll have to explain your intentions first.

I just read the beginners guide and they set a colour after they loaded the default shaded colour just to give it a colour. I just added it to follow the guide but If I remove it it says you have not set a colour so I added it back. Not sure why I would need to define a base colour on a material that already has a base colour defined which I’m assuming that default material does. I don’t know how to look at the material in the sdk to check I’ll create my own material and hopefully that will fix it.

Edit: I used my own material and removed the add colour line and it runs fine still not getting an object on the scene but at least it’s not throwing exceptions

The material you use depends on what you want. Lighting.j3md uses light, so you need lights in your scene or everything will be black. Unshaded.j3md doesn’t use lighting.

Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
material.setColor("Color", ColorRBGA.Blue);

Thanks,

I added a light in the init method still can’t see the box, but it is showing up as an object in the default debug text so it’s definitely there. I’ll play around a bit and see if I can get it to display.

Got it working, I was just using the wrong type of material as you said switched it to the unshaded for now and gave it a colour. I’ll test making a proper PBR texture which should work with my directional light.

Thanks for the help

1 Like

For PBR just use the PBR Material.

Material material = new Material(assetManager, "Common/MatDefs/Light/PBRLighting.j3md");
material.setColor("BaseColor", ColorRGBA.Blue);
material.setFloat("Roughness", 0.4f);
material.setFloat("Metallic", 0.001f);

But note you need at least a Directional light to see it. Ideally you want a LightProbe and a skybox if you want reflections. That’s a story for another day. Have fun :slight_smile: