Question

I am getting java.util.ConcurrentModificationException but I need to figure out which class gives me this exception. The code has numerous classes and packages and it is difficult to figure out where the error comes. The exception shows the problem of ArrayList. It doesn't catch exeption when I use exception handling in suspected areas.

Any way out?

Was it helpful?

Solution

If you're using a modern IDE, eclipse for example, then you can run the application in debug mode and set a breakpoint on any Exception. With the effect: the application will stop each time the exception is thrown (in the entire JVM) and you get a stack trace.

That makes it quite easy to identify the caller (and the actual thread, if it is a concurrency issue)


playn.java.JavaGroupLayer.paint(JavaGroupLayer.java:96)

That's the bad guy. It's the paint method your JavaGroupLayer class. It probably has a for loop that iterates through an array list and at one point it detects, that the list has been modified.

Do you use threads in your swing application? In that case, double check that they do not modify the layout.

OTHER TIPS

Usually start at the top of stack trace, and the first class that you wrote/recognize as yours should be your entry point. Afterwards, you probably make calls in other classes/methods, so you can follow the stacktrace and hence the offending code.

Additionally : What you are probably doing (I have done that in the past myself) is trying to modify a collection, while iterating over it. This is what concurrent modification means. Try to see where you are doing that and should be it.

Go through the stack trace from the top to bottom and the first class that belongs to your source code is the one that has received the exception from an ArrayList trying to do something illegal with it. Then check which threads are accessing that instance of an ArrayList in the same time and protect it with synchronized methods or synchronized blocks.

A common mistake is to discard the exception, only read the message or toString the exception. If you print the stack trace, you will be able to see exactly where (the class, the method and the line) of each level of the stack.

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
 at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:782)
 at java.util.ArrayList$Itr.next(ArrayList.java:754)
 at playn.java.JavaGroupLayer.paint(JavaGroupLayer.java:96)
 at playn.java.JavaPlatform$1.paint(JavaPlatform.java:222)
 at javax.swing.JComponent.paintToOffscreen(JComponent.java:5138)
 at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:‌​1454)
 at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1385)

In your IDE, you should be able to click on each line to see the code involved.

Given this is in a package, it is more likely that the problem is that you have used the library incorrectly. It appears that plyn is not mult-thread safe and you have a attempted to change a data structure in another thread instead of using the swap AWT thread.

Identify the list, then either change the iteration to use list iteration (for i=0; i < list.size(); i++) rather than Iterator iteration (for x:list)

Or you can make a shallow copy of the list and iterate that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top