Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #26827 > unrolled thread

Odd ConcurrentModificationException

Started byKnute Johnson <september@knutejohnson.com>
First post2013-12-10 09:41 -0800
Last post2013-12-10 13:10 -0800
Articles 3 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Odd ConcurrentModificationException Knute Johnson <september@knutejohnson.com> - 2013-12-10 09:41 -0800
    Re: Odd ConcurrentModificationException Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-12-10 14:22 -0500
    Re: Odd ConcurrentModificationException Knute Johnson <september@knutejohnson.com> - 2013-12-10 13:10 -0800

#26827 — Odd ConcurrentModificationException

FromKnute Johnson <september@knutejohnson.com>
Date2013-12-10 09:41 -0800
SubjectOdd ConcurrentModificationException
Message-ID<l87jo5$5vt$1@dont-email.me>
I'm getting an odd ConcurrentModificationException in a MouseListener 
when I try to remove an element from an ArrayList.  The really strange 
thing about it is that the exception occurs every other time I call 
remove().  The ArrayList is only accessed on the EDT and I tried 
wrapping it in Collections.synchronizedList but that made no difference.

The ArrayList variable is boxes and the type is DisplayBox, an extended 
JComponent.  The error shows the line number of the for instruction but 
actually fails on the remove(box) call.

Any ideas on where to start looking?

Thanks,

knute...


     public void mousePressed(MouseEvent me) {
         if (me.getButton() == MouseEvent.BUTTON1) {
             System.out.println("Mouse1 Down");
             mouseDown = true;
             startX = me.getX();
             startY = me.getY();
         } else if (me.getButton() == MouseEvent.BUTTON3) {
             System.out.println("Mouse3 Down");
             for (DisplayBox box : boxes) {
                 if (box.getBounds().contains(me.getX(),me.getY())) {
                     remove(box);
                     boxes.remove(box);
                 }
             }
             repaint();
         }
     }

Mouse3 Down
Exception in thread "AWT-EventQueue-0" 
java.util.ConcurrentModificationException

         at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
         at java.util.ArrayList$Itr.next(Unknown Source)
         at 
com.knutejohnson.stationcasinos.backgroundgenerator.Yard$1.mousePress
ed(Yard.java:47)
         at java.awt.Component.processMouseEvent(Unknown Source)
         at javax.swing.JComponent.processMouseEvent(Unknown Source)
         at java.awt.Component.processEvent(Unknown Source)
         at java.awt.Container.processEvent(Unknown Source)
         at java.awt.Component.dispatchEventImpl(Unknown Source)
         at java.awt.Container.dispatchEventImpl(Unknown Source)
         at java.awt.Component.dispatchEvent(Unknown Source)
         at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown 
Source)
         at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
         at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
         at java.awt.Container.dispatchEventImpl(Unknown Source)
         at java.awt.Window.dispatchEventImpl(Unknown Source)
         at java.awt.Component.dispatchEvent(Unknown Source)
         at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
         at java.awt.EventQueue.access$200(Unknown Source)
         at java.awt.EventQueue$3.run(Unknown Source)
         at java.awt.EventQueue$3.run(Unknown Source)
         at java.security.AccessController.doPrivileged(Native Method)
         at 
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
         at 
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
         at java.awt.EventQueue$4.run(Unknown Source)
         at java.awt.EventQueue$4.run(Unknown Source)
         at java.security.AccessController.doPrivileged(Native Method)
         at 
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
         at java.awt.EventQueue.dispatchEvent(Unknown Source)
         at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown 
Source)
         at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
         at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown 
Source)
         at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
         at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
         at java.awt.EventDispatchThread.run(Unknown Source)
-- 

Knute Johnson

[toc] | [next] | [standalone]


#26830

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-12-10 14:22 -0500
Message-ID<l87pl6$f1q$1@dont-email.me>
In reply to#26827
On 12/10/2013 12:41 PM, Knute Johnson wrote:
> I'm getting an odd ConcurrentModificationException in a MouseListener
> when I try to remove an element from an ArrayList.  The really strange
> thing about it is that the exception occurs every other time I call
> remove().  The ArrayList is only accessed on the EDT and I tried
> wrapping it in Collections.synchronizedList but that made no difference.
>
> The ArrayList variable is boxes and the type is DisplayBox, an extended
> JComponent.  The error shows the line number of the for instruction but
> actually fails on the remove(box) call.

     What's your evidence for the "actually fails" location, given
that the JVM says otherwise?

> Any ideas on where to start looking?
>
> Thanks,
>
> knute...
>
>
>      public void mousePressed(MouseEvent me) {
>          if (me.getButton() == MouseEvent.BUTTON1) {
>              System.out.println("Mouse1 Down");
>              mouseDown = true;
>              startX = me.getX();
>              startY = me.getY();
>          } else if (me.getButton() == MouseEvent.BUTTON3) {
>              System.out.println("Mouse3 Down");
>              for (DisplayBox box : boxes) {
>                  if (box.getBounds().contains(me.getX(),me.getY())) {
>                      remove(box);
>                      boxes.remove(box);
>                  }
>              }
>              repaint();
>          }
>      }

     The "for-each" construct (for an Iterable as opposed to an
array) expands more or less like

	for (Thing thing : somethingIterableOfThing) {
	    // do stuff
	}

becomes

	for (Iterator<Thing> it = somethingIterableOfThing.iterator();
	     it.hasNext(); ) {
	    Thing thing = it.next();
	    // do stuff
	}

If "do stuff" modifies "somethingIterableOfThing", *that's* the
ConcurrentModificationException, and the Iterator throws up next
time you use it.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#26831

FromKnute Johnson <september@knutejohnson.com>
Date2013-12-10 13:10 -0800
Message-ID<l87vv2$nuf$1@dont-email.me>
In reply to#26827
On 12/10/2013 9:49 AM, Stefan Ram wrote:
> Knute Johnson <september@knutejohnson.com> writes:
>> I'm getting an odd ConcurrentModificationException in a MouseListener
>> when I try to remove an element from an ArrayList.  The really strange
> ...
>>              for (DisplayBox box : boxes) {
> ...
>>                      boxes.remove(box);
>
>    This is an FAQ. Try using the iterator directly and then use Iterator#remove.
>

Stefan:

I don't know why my previous reply didn't show up but you are correct, I 
am seriously confused.  But your reply did allow me to correct my problem.

Thanks,

-- 

Knute Johnson

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web