这可能是一个非常令人困惑的标题,但是我很困惑。 我在做游戏,并且爆炸。我只是遇到了这个愚蠢的问题。 这是爆炸的代码(不包括包装和进口物品):
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;
}
}
当我尝试将其删除时会出错(在第25行)。
这是处理程序类的代码(不包括包和导入):
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++) {
}
}
} 这是游戏类(生成它的地方)的构造函数:
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));
}
它给了我这个错误:
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)
不要介意软件包的长名称。
如何解决此意外错误?