Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ConcurrentModificationException when painting component  RSS feed

 
Kees Jacobsen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm making a small card game in Java with Swing, and I've run into a problem.
I have the following code when you drag with your mouse:



What it's supposed to do, is when you first click on a stack of cards, it removes the stack of cards from the field, and then puts it on the drag panel.
This is the DragPanel class:


Which is calling a method from CardImager, which doesn't modify the card in any way.
Do any of you have any insight?
 
Henry Wong
author
Sheriff
Posts: 22832
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kees Jacobsen wrote:
Which is calling a method from CardImager, which doesn't modify the card in any way.
Do any of you have any insight?



It is not "card" -- it is "cards". You are not allowed to modify the List of cards while you are iterating through it. If you want to modify the list, you are only allowed to do it via the iterator, which you haven't exposed in your code.

Henry
 
Kees Jacobsen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also not modifying the list of cards - it's completely independent on input.
 
Henry Wong
author
Sheriff
Posts: 22832
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kees Jacobsen wrote:It's also not modifying the list of cards - it's completely independent on input.


Well, something is modifying that list. That error occurs when the list is modified while iteration is taking place -- and that modification can come from your code, or some other code not related to your method call, or thread of execution.

Henry
 
Kees Jacobsen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the problem is, I don't know what and why.
Without any body, the for loop where the stackTrace points to still causes problems.
Doing anything else, like printing the size of the 'cards' array, also causes a ConcurrentModificationException.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!