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:
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;
/**
* Shader, for lighting.
* @author Senior
*/
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;
//Next line is problem line.
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:
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:
public World(Main main) {
this.setMain(main);
//Set up workers.
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.