NullPointerException at Slick2D Image constructor

Hey,
I’m trying to create an image of a space ship that consist of different parts,
but I’m getting a NullPointerException at the Image(int, int) constructor.

[quote]Exception in thread “Client” java.lang.NullPointerException
at org.lwjgl.opengl.GL11.glGenTextures(GL11.java:1372)
at org.newdawn.slick.opengl.InternalTextureLoader.createTextureID(InternalTextureLoader.java:106)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:359)
at org.newdawn.slick.opengl.InternalTextureLoader.createTexture(InternalTextureLoader.java:343)
at org.newdawn.slick.opengl.InternalTextureLoader.createTexture(InternalTextureLoader.java:329)
at org.newdawn.slick.Image.(Image.java:238)
at net.aichix3.devrays.Ship.getSprite(Ship.java:32)
at net.aichix3.devrays.Shipslot.(Shipslot.java:29)
at net.aichix3.devrays.Network$1.received(Network.java:74)
at com.esotericsoftware.kryonet.Connection.notifyReceived(Connection.java:270)
at com.esotericsoftware.kryonet.Client.update(Client.java:296)
at com.esotericsoftware.kryonet.Client.run(Client.java:332)
at java.lang.Thread.run(Unknown Source)
[/quote]

static Image getSprite(ShipAspect a) {
		try {
			Image s = new Image(Game.i.get("ship" + a.id).getWidth() + 200, Game.i.get("ship" + a.id).getHeight() + 200); // Ship.java:32 // a.id == 3 // Game.i.get("ship3") != null
// new Image(200, 200) does not work too		
	Graphics sg = s.getGraphics();
			sg.drawImage(Game.i.get("ship" + a.id), 100, 100);
			sg.drawImage(Game.i.get("w1"), WEAPONS[1].x, WEAPONS[1].y);
			sg.flush();
			return s;
		} catch (SlickException e) {
			e.printStackTrace();
			return null;
		}
	}

What’s wrong with the code? =/

You’re trying to create an OpenGL image outside of the slick thread - you can’t do that.

Cheers,

Kev

Ah, thanks. :slight_smile:
The ship generation method is called by the network thread - after the server notifies the client about ship changes.
How could the network thread tell the Slick thread to call getSprite() ?

How does your network thread normally talk to your rendering thread safely?

Cheers,

Kev

The network thread does not have to talk to the slick thread at all, at least not yet.
I currently use CopyOnWriteArrayList to prevent concurrent using of my variables.

I’d stick something in that list then to tell the slick thread to load the image.

Cheers,

Kev

Ok, every ship has synchronized boolean isUptodate now which will be set to true in the network thread and evaluated in the next frame of the Slick thread. I hope the synchronized-modifer is enough to prevent exceptions because of concurrent using.

Thanks for your help. :slight_smile:

Oh, another thing:
Is there a way to speed up my method?

Does nobody have some optimizations for the above method? Creating two sprites decreases my FPS rate from ~ 100 to 6 FPS.

Don’t load images in your game loop.

It would be nice to use this method every frame to support different scalings for smaller screens.

But you only need to scale once. As soon as you know the target resolution load the images and do the scaling and store the scaled images.

From then on you’re fine using the scaled images.

I bypassed Image.getGraphics() by using a static Graphics instance,
this way is quite fast. :slight_smile: