Im getting this error when i run my program, Its running 4 instances of the same class, the code where the error is comign from is:
(tillQueue is a Queue of type person ( Queue<Person> tillQueue = new LinkedList<Person>(); )
I tried making the function synchronized but im getting the same error. Does anyone know how to fix this?
ALSO im getting some nullpointer exceptions on the for-each loop. Why is that happening, surely the loop just cycles through the tillQueue?
Line 61 corresponds to: for( Person o: tillQueue)
line 66 corresponds to: switch(o.getType())
So you must have more than one thread working on that list, because it's clear that the code you posted doesn't modify the list. And something is putting null values into the list, whereas your posted code is assuming it's only going to get references to actual Person objects and not nulls.
The documentation for Queue says
Queue implementations generally do not allow insertion of null elements, although some implementations, such as LinkedList, do not prohibit insertion of null. Even in the implementations that permit it, null should not be inserted into a Queue, as null is also used as a special return value by the poll method to indicate that the queue contains no elements.
so you should hunt down the code which is putting nulls into the queue and fix it so it doesn't do that. Also: you chose to describe your list as a Queue. The usual way to handle a Queue is to have two processes, a producer and a consumer. The producer does nothing but add entries at the end of the queue, and the consumer does nothing but pull entries off the beginning of the queue. If you really meant to have a Queue, perhaps your should restructure your code to work that way.
To simulate that discussion, you can force the error to occur by adding to "o" inside the loop.
This may be what is occurring via another thread.
For further debugging, you could modify the "add" function in a class derived from linkedlist to print the time/thread name of the addition. Then, also print the time / thread name at the start and end of your loop. Or, if you only add to "o" in a small number of locations, just print the time/thread at each add.
Some potential trivial fixes include
1) In drawPerson, copy the list to an array inside a sychronized block, sychronized on "o", and then iterate over the array instead of iterating over the list.
2) or, during creation of "o" synchronize the list via see linkedList.
3) You could live with the error, just think what you want to do after catching the exception in the code inside the drawPerson function. Possibly you recall the member function from the catch block, a finite number of times to prevent a race/trap condition, and if the finite number of times is exceeded, just give up or throw an exception... I don't think this is a good recommendation, but may lead you to think of an appropriate action to take when the exception is caught.
4) Put the loop inside a block that is synchronized on "o".
You might investigate further about why synchronizing the function did not correct the error, printing time/thread when drawPerson is called, along with printing modifications to "o", with/without synchronized function.
Heres the code incase someone comes accross a similar error in the future:
i also synchronized tillQueue