That was probably a very confusing title, but I'm pretty confused. I am making a game, and there are explosions. I just ran into this one stupid problem. Here's my code for the explosions (without the package and imports):
public class LBExplosion extends GameObject{
private int tick;
private int startSize,endSize,size;
Handler handler;
public LBExplosion(float x, float y, ID id, int startSize, int endSize, Handler handler) {
super(x, y, id);
// TODO Auto-generated constructor stub
this.startSize = startSize;
this.endSize = endSize;
size = startSize;
}
@Override
public void tick() {
// TODO Auto-generated method stub
tick++;
size++;
if (size >= endSize) {
this.handler.removeObject(this); //here
}
}
@Override
public void render(Graphics g) {
// TODO Auto-generated method stub
if (tick % 2 == 0) {
g.setColor(Color.white);
} else if (tick % 2 == 1) {
g.setColor(Color.red);
}
g.fillRect((int) x - size/2, (int) y - size/2, size, size);
}
@Override
public Rectangle getBounds() {
// TODO Auto-generated method stub
return null;
}
}
And it errors when I try to remove it (at line 25).
Here's the code for the handler class (without the package and imports):
public class Handler {
LinkedList <GameObject> objList = new LinkedList <GameObject>();
public void tick() {
for (int i = 0; i < objList.size(); i++) {
GameObject tempObject = objList.get(i);
tempObject.tick();
}
}
public void render(Graphics g) {
for (int i = 0; i < objList.size(); i++) {
GameObject tempObject = objList.get(i);
tempObject.render(g);
}
}
public void addObject(GameObject object) {
objList.add(object);
}
public void removeObject(GameObject object) {
objList.remove(object);
}
public void removeAllEnemies() {
for (int i = 0; i < objList.size(); i++) {
}
}
} And here's the constructor for the game class (where it's spawned):
public Game() {
handler = new Handler();
hud = new HUD();
spawner = new Spawner(handler);
this.addKeyListener(new KeyInput(handler));
new Window(WIDTH,HEIGHT,"Game", this);
handler.addObject(new Player (16, 16, ID.Player,hud,handler));
handler.addObject(new LBExplosion (300, 316, ID.LBExplosion,0,64,handler));
}
It gives me this error:
Exception in thread "Thread-2" java.lang.NullPointerException
at com.wave.noah.two.p.a.c.k.a.g.e.something.main.LBExplosion.tick(LBExplosion.java:26)
at com.wave.noah.two.p.a.c.k.a.g.e.something.main.Handler.tick(Handler.java:12)
at com.wave.noah.two.p.a.c.k.a.g.e.something.main.Game.tick(Game.java:80)
at com.wave.noah.two.p.a.c.k.a.g.e.something.main.Game.run(Game.java:62)
at java.lang.Thread.run(Unknown Source)
Don't mind the long package name.
How do I fix this unexpected error?