Materials broken - assetManager, loadTexture

I’m using the jMonkey Platform. When I assign materials like in this tutorial, I get runtime errors. For example, the following code compiles:


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.scene.shape.Box;
import com.jme3.texture.Texture;

public class Main extends SimpleApplication {

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

  @Override
  public void simpleInitApp() {

    /** A simple textured cube -- in good MIP map quality. */
    Box boxshape1 = new Box(new Vector3f(-3f,1.1f,0f), 1f,1f,1f);
    Geometry cube = new Geometry("My Textured Box", boxshape1);
    Material mat_stl = new Material(assetManager, "Common/MatDefs/Misc/SimpleTextured.j3md");
    Texture tex_ml = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
    mat_stl.setTexture("m_ColorMap", tex_ml);
    cube.setMaterial(mat_stl);
    rootNode.attachChild(cube);
  }
}



However, the program crashes with this in the log:

java.lang.NullPointerException
        at com.jme3.material.Material.setTexture(Material.java:240)
        at mygame.Main.simpleInitApp(Main.java:25)
        at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:141)
        at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:102)
        at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:147)
        at java.lang.Thread.run(Thread.java:619)



But, the method of assigning materials from this tutorial works fine, creating a plain blue cube:

/** A blue cube **/
    Box boxshape1 = new Box(new Vector3f(-3f,1.1f,0f), 1f,1f,1f);
    Geometry cube = new Geometry("My Textured Box", boxshape1);
    Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
    mat1.setColor("m_Color", ColorRGBA.Blue);
    cube.setMaterial(mat1);
    rootNode.attachChild(cube);



I have a poor understanding of the assetManager and material classes, which I think is responsible for the above code working fine even in the example packages. Any help would be appreciated!

You can identify what the problem is by looking at what line it happens.

The exception happens here:

mat_stl.setTexture("m_ColorMap", tex_ml);


And is a NullPointerException, so that means tex_ml is null, the texture has failed to load.
If you check the jME log you'll probably see something like this:
"Cannot locate asset: Interface/Logo/Monkey.jpg".

This particular file is located inside the jme3testdata.jar, since there were no errors loading the materials, I guess that means jme3testdata.jar is not on the classpath. The jMP should include it by default, try reinstalling jMP and see if it helps.

I deleted all the folders and re-downloaded the file from here, and then started a new project using BasicGame. I created a new package called hello06 and a new class called HelloMaterial. Then, I pasted the code from the Hello Material tutorial.



I don’t know if this is relevant, but this line:


TangentBinormalGenerator.generate(rock);           // for lighting effect



would not compile. It says, "cannot find symbol..." After removing that section, I compiled but got the same problem:

WARNING DesktopAssetManager 2:36:05 PM Cannot locate resource: Interface/Logo/Monkey.jpg
SEVERE Application 2:36:05 PM Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
        at com.jme3.material.Material.setTexture(Material.java:240)
        at hello06.HelloMaterial.simpleInitApp(HelloMaterial.java:30)
        at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:141)
        at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:102)
        at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:147)
        at java.lang.Thread.run(Thread.java:619)
BUILD SUCCESSFUL (total time: 3 seconds)



Line 30 is this:

mat_stl.setTexture("m_ColorMap", tex_ml);



Have I set something up wrong? The example projects seems to work fine with textures, but as soon as I copy the code into my project, it crashes.

This is an issue for normen to address. Like I said it seems there's a jar that is not being included in the list of libraries for some reason.

just one sidequestion as I see the above, why does jme3 even try to ignore not locateable ressources ? wouldn't there be a perfect place to throw a RuntimeException, as it will crash anyway later, just make it more difficult to see the one line causing it?



The tangentbionominal line, is mostly for shader materials to look correctly, it is not needed to run.



Als are you sure the texture you try to load exist there, the path is inside the ressourcelocator path's and it is spelled exactly identicall? JaVa CaReS for UPPER and lowercase , As i found out the hard way too,(eclipese btw ignores case sometimes, sometimes not)

Empire Phoenix said:

just one sidequestion as I see the above, why does jme3 even try to ignore not locateable ressources ? wouldn't there be a perfect place to throw a RuntimeException, as it will crash anyway later, just make it more difficult to see the one line causing it?

The tangentbionominal line, is mostly for shader materials to look correctly, it is not needed to run.

Als are you sure the texture you try to load exist there, the path is inside the ressourcelocator path's and it is spelled exactly identicall? JaVa CaReS for UPPER and lowercase , As i found out the hard way too,(eclipese btw ignores case sometimes, sometimes not)


Thanks. The exact same code runs in the example files, but when I copy/paste it to a new project, it doesn't work. Also, I'm using the jMonkey platform, so it can't be a problem with case. I don't know if I'm setting up the project wrong, or if there's another problem.

Just an update: I discovered that it's incredibly easy to use jME3 with Eclipse. I tested it out and everything (including the above code, minus the single problem line) works fine. I prefer Eclipse over NetBeans, so I don't need a solution any longer, but for future reference the problem with the Platform is unresolved.