I am programming a simple Top Down Shooter right now. The player is able to hold the down mouse to continously shoot. Since there is no native way to check if the mouse button is held down I implemented this solution: https://stackoverflow.com/a/6828990/3716866
This works perfectly fine. But the problem is that I put each bullet object and each GameObject (enemies, player, barrels, etc.) in a Linked List (two lists one for the objects, one for the bullets). When you shoot a bullet it get's added to the LinkedList. Each runthrough of the gameloop the whole List get's checked through for collision. After firing about 2 shots the game crashes with a Null Pointer Exception. I think the problem is, since I use a second thread to implement the mouse button hold down, that a bullet object is getting created while in the other thread it checks through the list. Since I am checking through the list, while the size is getting altered it is checking through the list, it crashes. But that is just my theory. Anybody got an idea on how to keep them at synch?
If you need more code etc. just tell me and I will upload it. I just wasn't sure what I should post. So before I spam everything I wanted to wait for your feedback.
The Exception:
Exception in thread "Thread-2" java.lang.NullPointerException
at shooter.main.Handler.checkForCollision(Handler.java:50)
at shooter.main.Handler.tick(Handler.java:41)
at shooter.main.Game.tick(Game.java:189)
at shooter.main.Game.run(Game.java:290)
at java.lang.Thread.run(Unknown Source)
Handler Class that runs all the tick and update methods for every Object, including collision:
My MouseListener Class where the other Thread is implemented:
My Main Game class, although I don't think it will be very important for this problem:
My abstract GameObject Class:
Example of a GameObject:
My abstract Ammo Class:
Example of a Bullet:
How a shoot method looks like:
public void shoot() {
//play sound effect
// fire bullet
game.getHandler().addBullet(new Bullet9mm(game.getHandler().getPlayer().getX(), game.getHandler().getPlayer().getY(), game.getBulletImageManager(), game));
// remove 1 bullet from magazine
game.getHandler().getPlayer().getCurWeapon().setMagAmmo(-1);
}
I hope I provided every info necessary. If you still need something else from my code just tell me.