File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Can someone help me figure this out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Can someone help me figure this out" Watch "Can someone help me figure this out" New topic
Author

Can someone help me figure this out

Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
My game is almost complete and I am so proud of it but I just discovered a bug that I don�t know how to fix, Im hoping you can help me with it.

This game is a typical space shooter whith all sorts of alien ships with different kind of weapons. The bug I discovered seems to be isolated around one of the boss aliens (I call it simply carrier). The carrier has 4 weapons (two lasers, two plasmas) and it releases couple of drones into the game every 5 sec or so, the drones have only one laser gun.

Every alien on screen are kept in a single ArrayList, when a new alien spawns it is added to the end of the list, when an alien is destroyed it�s index in the list is removed.
This bug seems to happen when I fire back at the aliens, after some shooting the game stops and gives the said exception. I�ve made a little debug code to try to get my foot into the door this is the results when the exception occurs:

ListSize 14 AlienNr 7 Name Drone WeaponNr 0 Weapon Laser I
ListSize 16 AlienNr 14 Name Drone WeaponNr 0 Weapon Laser I
ListSize 16 AlienNr 15 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 6 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 8 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 11 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 12 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 2 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 4 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 5 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 1 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 10 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 7 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 9 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 0 Name Carrier WeaponNr 4 Weapon Laser III

ListSize 15 AlienNr 0 Name Carrier WeaponNr 5 Weapon Laser III

ListSize 15 AlienNr 13 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 14 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 0 Name Carrier WeaponNr 2 Weapon Plasma Tu
rret
ListSize 15 AlienNr 0 Name Carrier WeaponNr 3 Weapon Plasma Tu
rret
ListSize 15 AlienNr 6 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 8 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 11 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 12 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 2 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 4 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 5 Name Drone WeaponNr 0 Weapon Laser I
ListSize 15 AlienNr 1 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 9 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 6 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 8 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 12 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 13 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 5 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 7 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 10 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 11 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 2 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 4 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 1 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 3 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 9 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 6 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 8 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 0 Name Carrier WeaponNr 4 Weapon Laser III

ListSize 14 AlienNr 0 Name Carrier WeaponNr 5 Weapon Laser III

ListSize 14 AlienNr 13 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 12 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 7 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 5 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 11 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 2 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 4 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 1 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 3 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 9 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 6 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 8 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 13 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 12 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 7 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 5 Name Drone WeaponNr 0 Weapon Laser I
ListSize 14 AlienNr 11 Name Drone WeaponNr 0 Weapon Laser I
ListSize 16 AlienNr 2 Name Drone WeaponNr 0 Weapon Laser I
ListSize 16 AlienNr 4 Name Drone WeaponNr 0 Weapon Laser I
ListSize 16 AlienNr 1 Name Drone WeaponNr 0 Weapon Laser I
ListSize 16 AlienNr 14 Name Drone WeaponNr 0 Weapon Laser I
ListSize 16 AlienNr 15 Name Drone WeaponNr 0 Weapon Laser I
ListSize 16 AlienNr 9 Name Drone WeaponNr 0 Weapon Laser I
ListSize 16 AlienNr 0 Name Carrier WeaponNr 4 Weapon Laser III

ListSize 16 AlienNr 0 Name Carrier WeaponNr 5 Weapon Laser III

ListSize 16 AlienNr 8 Name Drone WeaponNr 0 Weapon Laser I
ListSize 16 AlienNr 0 Name Drone WeaponNr 2
0 Carrier
1 Drone
2 Drone
3 Drone
4 Drone
5 Drone
6 Drone
7 Drone
8 Drone
9 Drone
10 Drone
11 Drone
12 Drone
13 Drone
14 Drone
15 Drone
Exception in thread "Thread-6" java.lang.NullPointerException
at AlienAttack$MainTimer.run(AlienAttack.java:1078)
at java.lang.Thread.run(Thread.java:595)

The first entry is the alien list size you notice it increases by two, thats when the carrier releases two drones into the game.
The second entry shows the current alien position in the list.
The third entry is the aliens name.
The fourth entry show the number of the weapon the alien is currently firing.
Lastly the fifth entry shows the weapon name.

A new line printed each time an alien fires. You notice that the carrier is always the first entry in the list namely entry 0(because it entered the game first) until the exception occurs, something strange happened, look at the last line (in bold) suddently a drone has taken the first position in the list! There is a loop that checks wether the current alien has any more guns to fire from, drone only has one but the last line show turretNr 2 which is a reference to the carrier but ofcourse the drone doesn�t have that weapon so when calling getWeaponName() method in order to fill the last entry a NullPointException is thrown. When that happened I had the program print out the whole list and the alien names and it shows the carrier as it should be in position 0. But why did the drone take its place for that one split millisecond?

I guess it�s some synchronization issue, I am using a thread to with thread.sleep() to make the animation and a paint method to paint and a mouseListener to listen for mouse clicks.

Is it possible that there are so many loops and operations within the run() method of the thread that it can�t keep up with mouseListener or something? It�s the best I can think of.

I hope this hasn�t bored you too much, but I tried to be thorough in order for you to understand the problem better.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Are you using synchronization when necessary to protect your data? If one thread is modifying that ArrayList, in particular, then you must ensure that no other thread can access it during that time. The same goes for all the game entities, and any other objects that will be shared by multiple threads.


[Jess in Action][AskingGoodQuestions]
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
No im not, im kinda newbie regarding threads

I did a little reading and have a question, is synchronization related issues resolved simply by including [b]synchronized[b] in the description of the critical methods? Does java lock and open the access to the process automaticly or to I have to fiddle with the wait/notify/notifyAll methods?
[ January 11, 2006: Message edited by: Unnar Bj�rnsson ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

wait and notify are only for explicitly communicating between threads. Although they're very commonly used, you don't need to use them unless -- well, unless you need to. In your setup, it sounds like they're not needed yet.

Making every method synchronized is a heavyweight solution, and it's not guaranteed to be sufficient. There's unfortunately no shortcut to understanding Threads -- you have to think about the issues and do the right thing in your specific case. A good first step would be identifying resources that are used from multiple threads simultaneously, then considering how to protect them.

This tutorial is a good introduction to threads. Be sure to read the whole thing!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone help me figure this out
 
Similar Threads
A contest for fun (and bragging rights)
Parsing M*N matrix on Periphery
a swith/case question
Times Assignment
HashSet Magic