SmokeNWrite
|
 |
«
Posted
2012-02-09 22:40:06 » |
|
Hey, trying to create a simple new Image, blank, then add the graphics altered from another image and replace it with the image just made. Sounds simple but when I've been trying to create a new image or even access any Images graphics its not been working, throwing a null error every time, real confused at this been wrecking my brain for past hour or two. EDIT: this is shader class with the problem in it: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| package org.background.worker;
import java.util.Iterator;
import javax.swing.SwingWorker;
import org.background.LightProcessor; import org.model.World; import org.model.emitters.Emitter; import org.model.emitters.Light; import org.model.player.Player; import org.newdawn.slick.Color; import org.newdawn.slick.Graphics; import org.newdawn.slick.Image;
public class Shader extends SwingWorker<World, Void> {
private LightProcessor processor = null; public LightProcessor getProcessor() { return processor; }
public void setProcessor(LightProcessor processor) { this.processor = processor; }
public Shader(LightProcessor processor) { this.setProcessor(processor); }
@Override protected World doInBackground() throws Exception { if(processor.getWorld() == null) return null; World world = this.getProcessor().getWorld(); Player player = world.getPlayer(); if(player == null) return null; if(world.getLightEmitters().size() > 0) { for(Iterator<Emitter> it$ = world.getLightEmitters().descendingIterator(); it$.hasNext();) { Light l = (Light) it$.next(); if(l == null) continue; if(!l.getPosition().isWithin(player.getPosition(), (int)l.getRange())) continue; if(player.getIcon() == null) continue; try { Image image = null; image = world.getBlank(); if(image == null) continue; image = image.getScaledCopy(player.getIcon().getWidth(), player.getIcon().getHeight()); if(image == null) continue; Graphics g = image.getGraphics(); if(g == null) continue; Image pixel = null; for(int h = 0; h < player.getIcon().getHeight(); h++) { for(int w = 0; w < player.getIcon().getWidth(); w++) { pixel = player.getIcon().getSubImage(w, h, 1, 1); g.drawImage(pixel, w, h, Color.red); } } player.setIcon(image); } catch(Exception e) { e.printStackTrace(); } } } return world; } } |
In theory it should work but can't find the graphics here is the error: 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| java.lang.NullPointerException at org.newdawn.slick.opengl.pbuffer.GraphicsFactory.init(GraphicsFactory.java:40) at org.newdawn.slick.opengl.pbuffer.GraphicsFactory.createGraphics(GraphicsFactory.java:120) at org.newdawn.slick.opengl.pbuffer.GraphicsFactory.getGraphicsForImage(GraphicsFactory.java:91) at org.newdawn.slick.Image.getGraphics(Image.java:397) at org.background.worker.Shader.doInBackground(Shader.java:62) at org.background.worker.Shader.doInBackground(Shader.java:1) at javax.swing.SwingWorker$1.call(Unknown Source) at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at javax.swing.SwingWorker.run(Unknown Source) at org.background.LightProcessor.run(LightProcessor.java:37) at java.util.TimerThread.mainLoop(Unknown Source) at java.util.TimerThread.run(Unknown Source) |
And here is where the Image 'blank' is set: 1 2 3 4 5 6 7 8 9 10 11 12 13
| public World(Main main) { this.setMain(main); this.setProcessor(new Processor(this)); this.setLightProcessor(new LightProcessor(this)); try { this.setBlank(new Image("./data/blank.png", false, Image.FILTER_LINEAR)); .... } catch(SlickException e) { e.printStackTrace(); } } |
The world is initialized in the init() method of Main extends BasicGame. Any ideas? Peace.
|
|
|
|
aazimon
|
 |
«
Reply #1 - Posted
2012-02-09 22:53:40 » |
|
Is the image object null? If so it may not be finding the image. How are you loading the image?
|
|
|
|
SmokeNWrite
|
 |
«
Reply #2 - Posted
2012-02-09 23:02:55 » |
|
Was just trying to create a blank Image eg new Image(256, 256) to draw graphics on and create a image. The image is throwing a null at the moment but the graphics also does. I don't get what's happening the principle is simple. I've tried numerous different ways, ImageBuffer, loading a actual Image it all seems to be obsolete.  Peace
|
|
|
|
Games published by our own members! Check 'em out!
|
|
yuma
Junior Devvie   Medals: 2Projects: 1
Monkeys are listening
|
 |
«
Reply #3 - Posted
2012-02-09 23:09:38 » |
|
I think the problem is in the constructor of Image() (in the backtrace you see org.newdawn.slick.Image.<init>)
Are you sure player.getIcon().getWidth() and player.getIcon().getHeight() return the right size?
|
|
|
|
SmokeNWrite
|
 |
«
Reply #4 - Posted
2012-02-09 23:16:32 » |
|
Pretty sure and I've tried different constructors of Image and even images that I know work. When I try get Graphics from the players icon that would throw a null too.
Even the graphics I use to paint the images if I try setting that and using it there it still doesn't work. Rather confusing me.
|
|
|
|
ra4king
|
 |
«
Reply #5 - Posted
2012-02-10 06:04:37 » |
|
This looks like an old LWJGL bug I've seen a while ago. Are you using the latest version?
|
|
|
|
SmokeNWrite
|
 |
«
Reply #6 - Posted
2012-02-10 11:45:29 » |
|
I thought I was I'll download new LWJGL Thanks mate.
|
|
|
|
SmokeNWrite
|
 |
«
Reply #7 - Posted
2012-02-10 11:59:52 » |
|
Well I updated my LWJGL from 2.8.3 - 2.8.4 and downloaded (newer?) version of Slick. Still doesn't work :/  Help?
|
|
|
|
yuma
Junior Devvie   Medals: 2Projects: 1
Monkeys are listening
|
 |
«
Reply #8 - Posted
2012-02-10 12:09:32 » |
|
Is your OpenGL context created? I think with LWJGL you have to call Display.create(), but it is some time since I last used it. Other thing I could think of is enabling textures in OpenGL: 1
| import org.lwjgl.opengl.GL11.glEnable(GL11.GL_TEXTURE_2D); |
|
|
|
|
SmokeNWrite
|
 |
«
Reply #9 - Posted
2012-02-10 12:18:14 » |
|
Is your OpenGL context created? I think with LWJGL you have to call Display.create(), but it is some time since I last used it. Other thing I could think of is enabling textures in OpenGL: 1
| import org.lwjgl.opengl.GL11.glEnable(GL11.GL_TEXTURE_2D); |
Tried both don't seem to work. This my initialize method; 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
yuma
Junior Devvie   Medals: 2Projects: 1
Monkeys are listening
|
 |
«
Reply #10 - Posted
2012-02-10 12:28:11 » |
|
Ooops, I forgot you are using Slick. I have never used it (except for the utils to load PNG textures). Some random things that probably wont work (sorry :-(): - Do you try to create the image AFTER this.getApp().start() is called?
- The init() method is of a class inherited from org.newdawn.slick.BasicGame? Although I don't think it's relevant for this error, you could append the @Override tag to it, so you get a warning if it doesn't override for some reason.
- If your class is inherited from *.BasicGame, do you call the super() constructor on your constructor?
|
|
|
|
SmokeNWrite
|
 |
«
Reply #11 - Posted
2012-02-10 12:37:36 » |
|
Ooops, I forgot you are using Slick. I have never used it (except for the utils to load PNG textures). Some random things that probably wont work (sorry :-(): - Do you try to create the image AFTER this.getApp().start() is called?
- The init() method is of a class inherited from org.newdawn.slick.BasicGame? Although I don't think it's relevant for this error, you could append the @Override tag to it, so you get a warning if it doesn't override for some reason.
- If your class is inherited from *.BasicGame, do you call the super() constructor on your constructor?
Yeah I create after this.getApp().start() and the init method of that is not thats in Main which has the superclass BasicGame and it is calling Super(String). Really not sure, if I was in java2d this would be easy. But slick is awesome ¬.¬
|
|
|
|
H3rnst
Senior Newbie  Medals: 1
|
 |
«
Reply #12 - Posted
2012-02-10 13:47:54 » |
|
First of all I'd like to say that I'm a complete newbie. Perhaps that's why I find something odd in the next piece of code: 1 2 3 4 5 6 7 8 9 10 11 12
| try { Image image = null; try { image = new Image(256,256); } catch (SlickException se) {se.printStackTrace(); }
image.getScaledCopy(player.getIcon.getWidth(), player.getIcon.getHeight());
Graphics g = image.getGraphics(); Image pixel = null; |
You're not setting another Image with the return of getScaledCopy(). Does this method sets 'image' to it's scaled copy? Anyway I don't think it has something to do with your problem. But I do think that it might be a variable scope problem. Have you tried creating the 'image' outside the method and initializing it inside?
|
|
|
|
SmokeNWrite
|
 |
«
Reply #13 - Posted
2012-02-10 13:50:55 » |
|
First of all I'd like to say that I'm a complete newbie. Perhaps that's why I find something odd in the next piece of code: 1 2 3 4 5 6 7 8 9 10 11 12
| try { Image image = null; try { image = new Image(256,256); } catch (SlickException se) {se.printStackTrace(); }
image.getScaledCopy(player.getIcon.getWidth(), player.getIcon.getHeight());
Graphics g = image.getGraphics(); Image pixel = null; |
You're not setting another Image with the return of getScaledCopy(). Does this method sets 'image' to it's scaled copy? Anyway I don't think it has something to do with your problem. But I do think that it might be a variable scope problem. Have you tried creating the 'image' outside the method and initializing it inside? Oh no that was an accident I forgot to put image = image.getScaledCopy(). Yeah the image I have tried that the image in first post is made in init(). Yet again I'm just not sure what's going on, should be simple.
|
|
|
|
H3rnst
Senior Newbie  Medals: 1
|
 |
«
Reply #14 - Posted
2012-02-10 13:58:09 » |
|
You tried this: 1 2 3 4 5 6 7
| public void init(){ Image image; }
public void render(Graphics g){ image = new Image(256,256); } |
Or this (this is what I meant) 1 2 3 4 5 6 7 8 9
| Image image;
public void init(){ image = new Image(256,256); }
public void render(Graphics g){ image.resizeOrDrawThisImage(); } |
|
|
|
|
SmokeNWrite
|
 |
«
Reply #15 - Posted
2012-02-10 14:18:52 » |
|
This is in render and the image is created in init()  Window is crashed..
|
|
|
|
sproingie
|
 |
«
Reply #16 - Posted
2012-02-10 17:25:24 » |
|
It would help if you pasted your actual code instead of screenshots.
|
|
|
|
GabrielBailey74
|
 |
«
Reply #17 - Posted
2012-02-10 21:01:49 » |
|
Just rolled out of bed so I could be wrong/late. Define the variable you'd like to use to connect a image to "Sprite". Define the image/path, than link it to that Sprite variable. Than render that Sprite variable after linking the Image to it. 1 2 3 4 5 6 7
| public BufferedImage Sprite; public ImageIcon player = new ImageIcon("path/to/file.png");
public void paint(Graphics g) { Sprite = player.getImage(); g.drawIage(Sprite, 20, 20, null); } |
Hope it helps some 
|
|
|
|
ra4king
|
 |
«
Reply #18 - Posted
2012-02-10 22:20:44 » |
|
Yeah just post an SSCCE please 
|
|
|
|
SmokeNWrite
|
 |
«
Reply #19 - Posted
2012-02-11 12:11:20 » |
|
Just rolled out of bed so I could be wrong/late. Define the variable you'd like to use to connect a image to "Sprite". Define the image/path, than link it to that Sprite variable. Than render that Sprite variable after linking the Image to it. 1 2 3 4 5 6 7
| public BufferedImage Sprite; public ImageIcon player = new ImageIcon("path/to/file.png");
public void paint(Graphics g) { Sprite = player.getImage(); g.drawIage(Sprite, 20, 20, null); } |
Hope it helps some  Its slick not java 2d mate. And i'll edit my thread with sscce in a minute. edit; updated.
|
|
|
|
ra4king
|
 |
«
Reply #20 - Posted
2012-02-11 15:04:08 » |
|
This is line 40 in GraphicsFactory: 1
| fbo = GLContext.getCapabilities().GL_EXT_framebuffer_object; |
I have no idea how a NullPointer can be thrown there, unless this is an LWJGL bug where GLContext.getCapabilities() returns null.
|
|
|
|
SmokeNWrite
|
 |
«
Reply #21 - Posted
2012-02-11 15:55:58 » |
|
This is line 40 in GraphicsFactory: 1
| fbo = GLContext.getCapabilities().GL_EXT_framebuffer_object; |
I have no idea how a NullPointer can be thrown there, unless this is an LWJGL bug where GLContext.getCapabilities() returns null. Exactly what I mean, the error is totally confusing and shouldn't happen its a pretty simple procedure I'm trying and I have tried several ways around which have also failed. I can't paste anymore code neither cause I ain't at home. Would really appreciate it if somebody could figure this out.  Peacee
|
|
|
|
Matthias
|
 |
«
Reply #22 - Posted
2012-02-12 18:13:18 » |
|
GLContext.getCapabilities() return null when the current thread has no GL context active.
|
|
|
|
ra4king
|
 |
«
Reply #23 - Posted
2012-02-12 19:31:09 » |
|
*FACEPALM* Of course! @OP You're loading that image from another thread! You should do everything on the same thread as the rendering thread and after the GL Context has been initialized 
|
|
|
|
SmokeNWrite
|
 |
«
Reply #24 - Posted
2012-02-12 21:07:01 » |
|
*FACEPALM* Of course! @OP You're loading that image from another thread! You should do everything on the same thread as the rendering thread and after the GL Context has been initialized  Surprisingly I already thought of it but never did try it, I'll just make a trigger or something that I can set in that thread then activate in the other thread. Nice one mate (: EDIT: Wait don't know if that'll work because I tried getting graphics of a Image in render() and it makes the game freeze. So strange... :/
|
|
|
|
SmokeNWrite
|
 |
«
Reply #25 - Posted
2012-02-20 12:57:50 » |
|
Stilll not fixed!
Peace..
|
|
|
|
ra4king
|
 |
«
Reply #26 - Posted
2012-02-20 19:29:27 » |
|
Any more info you can give us about this?
|
|
|
|
SmokeNWrite
|
 |
«
Reply #27 - Posted
2012-03-02 11:37:18 » |
|
Not really sure what else there is to say everything else works and this would be simple in java2d.
There is a converter but then I would have to convert back lol
Peace
|
|
|
|
|