Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  NullPointerException at Slick2D Image constructor  (Read 1722 times)
0 Members and 1 Guest are viewing this topic.
Offline jaid

Senior Newbie





« Posted 2011-12-03 17:21:56 »

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.<init>(Image.java:238)
   at net.aichix3.devrays.Ship.getSprite(Ship.java:32)
   at net.aichix3.devrays.Shipslot.<init>(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)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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? =/
Offline kevglass

JGO Kernel


Medals: 188
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2011-12-03 19:54:12 »

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

Cheers,

Kev

Offline jaid

Senior Newbie





« Reply #2 - Posted 2011-12-03 20:06:55 »

Ah, thanks. Smiley
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() ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

JGO Kernel


Medals: 188
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2011-12-03 20:31:37 »

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

Cheers,

Kev

Offline jaid

Senior Newbie





« Reply #4 - Posted 2011-12-03 20:55:24 »

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.
Offline kevglass

JGO Kernel


Medals: 188
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #5 - Posted 2011-12-03 22:22:53 »

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

Cheers,

Kev

Offline jaid

Senior Newbie





« Reply #6 - Posted 2011-12-03 23:48:45 »

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. Smiley
Offline jaid

Senior Newbie





« Reply #7 - Posted 2011-12-04 00:44:41 »

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

Senior Newbie





« Reply #8 - Posted 2011-12-04 21:21:01 »

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

JGO Kernel


Medals: 352
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-12-04 22:55:08 »

Don't load images in your game loop.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jaid

Senior Newbie





« Reply #10 - Posted 2011-12-05 00:13:10 »

It would be nice to use this method every frame to support different scalings for smaller screens.
Offline thaaks
« Reply #11 - Posted 2011-12-05 07:41:53 »

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.

Offline jaid

Senior Newbie





« Reply #12 - Posted 2011-12-08 20:19:58 »

I bypassed Image.getGraphics() by using a static Graphics instance,
this way is quite fast. Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (20 views)
2014-10-25 15:46:29

Longarmx (53 views)
2014-10-17 03:59:02

Norakomi (47 views)
2014-10-16 15:22:06

Norakomi (35 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (69 views)
2014-10-14 00:39:48

TehJavaDev (69 views)
2014-10-14 00:35:47

TehJavaDev (61 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (47 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!