NPE when setting texture to material

I’m trying to load a texture and set it to a material using a way I’ve used before but I keep getting a strange NPE.

first I load said image;

public void setHoverTexture(String filePath) {
        try {
        File file = new File(filePath);
        image = ImageIO.read(file);
        this.hoverTexture = new Texture2D(imageLoader.load(image, true));
        } catch (IOException e) {
        System.out.println("failed to load hover texture");
        }
    }

No problems here everything loads fine with no errors.

Then I get an error when I try to set my material texture.

material.setTexture("ColorMap",hoverTexture);

I’ve tested setting the material with some custom hand drawn images using graphics2d and those work fine. The only was I’ve been able to get this to work is to create a whole new material then load the texture to it but it seems pointless to create a new material for each texture when I can just retexture the same material.

Jun 24, 2016 10:32:48 AM com.jme3.app.LegacyApplication handleError
SEVERE: Uncaught exception thrown in Thread[jME3 Main,6,main]
java.lang.NullPointerException
	at com.jme3.renderer.opengl.GLRenderer.setTexture(GLRenderer.java:2232)
	at com.jme3.material.Material.updateShaderMaterialParameters(Material.java:800)
	at com.jme3.material.Material.render(Material.java:960)
	at com.jme3.renderer.RenderManager.renderGeometry(RenderManager.java:616)
	at com.jme3.renderer.queue.RenderQueue.renderGeometryList(RenderQueue.java:266)
	at com.jme3.renderer.queue.RenderQueue.renderQueue(RenderQueue.java:302)
	at com.jme3.renderer.RenderManager.renderViewPortQueues(RenderManager.java:898)
	at com.jme3.renderer.RenderManager.flushQueue(RenderManager.java:781)
	at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:1097)
	at com.jme3.renderer.RenderManager.render(RenderManager.java:1153)
	at com.jme3.app.SimpleApplication.update(SimpleApplication.java:253)
	at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
	at com.jme3.system.lwjgl.LwjglCanvas.runLoop(LwjglCanvas.java:229)
	at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:232)
	at java.lang.Thread.run(Thread.java:745)

Maybe it is about the image format ?!
Does it work if you import your texture to your image-editing program and export it again ?

I found the problem

hoverTexture = new Texture2D();

This was in my constructor. I commented it out and it worked. I’m not sure but I think you may be able to create a new texture only once.

1 Like

Maybe one of the core devs can comment on that :smiley:

Most of the time the empty constructor (no parameters) is only for serialization (see javadoc).
This means that the values are set afterwards. As a user you should never use the serialization constructor.

In this case, hoverTexture maybe was a texture without content, actually maybe even without a buffer so there is this exception when trying to access the buffer.

2 Likes