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 =;
        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.


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 handleError
SEVERE: Uncaught exception thrown in Thread[jME3 Main,6,main]
	at com.jme3.renderer.opengl.GLRenderer.setTexture(
	at com.jme3.material.Material.updateShaderMaterialParameters(
	at com.jme3.material.Material.render(
	at com.jme3.renderer.RenderManager.renderGeometry(
	at com.jme3.renderer.queue.RenderQueue.renderGeometryList(
	at com.jme3.renderer.queue.RenderQueue.renderQueue(
	at com.jme3.renderer.RenderManager.renderViewPortQueues(
	at com.jme3.renderer.RenderManager.flushQueue(
	at com.jme3.renderer.RenderManager.renderViewPort(
	at com.jme3.renderer.RenderManager.render(
	at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(
	at com.jme3.system.lwjgl.LwjglCanvas.runLoop(

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.