Concurrent Modification Exception

Hello,

i got this Exception while deleting objects from an ArrayList.

I marked the spot where the error occured (as NetBeans say)

with an ->

//per UnitOfWorkInstance

private List<AbstractDomainObject> newObjects =new ArrayList<AbstractDomainObject>();

private List<AbstractDomainObject> dirtyObjects =new ArrayList<AbstractDomainObject>();

private List<AbstractDomainObject> removedObjects =new ArrayList<AbstractDomainObject>();

//Rollback the Transaction

publicvoid rollback(){

reloadDirty();

deleteNew();

restoreDeleted();

}

privatevoid deleteNew(){

->for(AbstractDomainObject object: newObjects){

logger.info(object.toString());

newObjects.remove(object);

}

}

privatevoid restoreDeleted(){

for(AbstractDomainObject object: removedObjects){

logger.info(object.toString());

//Add back into idmap!

IDMapRegistry.getMap(object.getClass()).addEntity(object);

}

}

privatevoid reloadDirty(){

for(AbstractDomainObject object: dirtyObjects){

logger.info(object.toString());

//object reloads its data from database

object.renew();

}

}

I dont know much about this exception, but if it has anything to do

whith threads. Iam not using additional threads at all!

Maybe i despite should synchronize all these methods?

Would that solve the problem?

I would really appreciate ur help

Thanks in advance

Yves

--

Here is the Stack-Trace:

Exception occurred during event dispatching:

java.util.ConcurrentModificationException

at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)

at java.util.AbstractList$Itr.next(AbstractList.java:343)

at de.yvestiesler.DataMapper.UnitOfWork.UnitOfWork.deleteNew(UnitOfWork.java:203)

at de.yvestiesler.DataMapper.UnitOfWork.UnitOfWork.rollback(UnitOfWork.java:197)

at de.yvestiesler.GUI.Frames.PositionManagement.PresentationModelPositionManagement.cancelAction(PresentationModelPositionManagement.java:73)

at de.yvestiesler.GUI.Frames.PositionManagement.ViewPositionManagement.btnCancelActionPerformed(ViewPositionManagement.java:359)

at de.yvestiesler.GUI.Frames.PositionManagement.ViewPositionManagement.access$400(ViewPositionManagement.java:23)

at de.yvestiesler.GUI.Frames.PositionManagement.ViewPositionManagement$3.actionPerformed(ViewPositionManagement.java:143)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6038)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)

at java.awt.Component.processEvent(Component.java:5803)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)

at java.awt.Container.dispatchEventImpl(Container.java:2102)

at java.awt.Window.dispatchEventImpl(Window.java:2429)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:177)

at java.awt.Dialog$1.run(Dialog.java:1039)

at java.awt.Dialog$3.run(Dialog.java:1091)

at java.security.AccessController.doPrivileged(Native Method)

at java.awt.Dialog.show(Dialog.java:1089)

at java.awt.Component.show(Component.java:1419)

at java.awt.Component.setVisible(Component.java:1372)

at java.awt.Window.setVisible(Window.java:801)

at java.awt.Dialog.setVisible(Dialog.java:979)

at de.yvestiesler.GUI.Frames.PositionManagement.ViewPositionManagement$12.run(ViewPositionManagement.java:403)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

[6469 byte] By [congora] at [2007-11-26 18:48:43]
# 1

I dont know but is this the solution?:

private void deleteNew(){

Iterator<AbstractDomainObject> newIter = newObjects.iterator();

while(newIter.hasNext()){

logger.info(newIter.next().toString());

newIter.remove();

}

//The following code can lead to an CocurrentModificationException

//Since while iterating, removals are just allowed through the

//iterator.remove() method!!!!

//for(AbstractDomainObject object: newObjects){

//logger.info(object.toString());

//newObjects.remove(object);

//}

}

congora at 2007-7-9 6:22:45 > top of Java-index,Core,Core APIs...
# 2

I dont know but is this the solution?:

private void deleteNew(){

Iterator<AbstractDomainObject> newIter = newObjects.iterator();

while(newIter.hasNext()){

logger.info(newIter.next().toString());

newIter.remove();

}

//The following code can lead to an CocurrentModificationException

Hello dear

See the bold portion

first u are iterating a list newiter

then in the same while loop u are removing its contents

Same time u are using twice

that is why it is giving concurrent Modification Exception

just do one thing

Make second bold line comment and run the program u wont get it

bye

rakesh

rakesh_thakura at 2007-7-9 6:22:45 > top of Java-index,Core,Core APIs...
# 3

Hi Rakesh,

newIter is not a list, but an Iterator.

It works with that solution I wrote in the second post.

I think I got it:

While Iterating through a collection, you cannot modify its structure,

except through the functions of the Iterator (if it has any, like remove).

Yves

congora at 2007-7-9 6:22:45 > top of Java-index,Core,Core APIs...
# 4

Excerpt from the JavaDoc for class "ArrayList" clearly explains the problem

"The iterators returned by this class's iterator and listIterator methods are fail-fast: if list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future."

ebrahim.faisala at 2007-7-9 6:22:45 > top of Java-index,Core,Core APIs...
# 5
Could Please help me.. By default I am storing the date value into the database in a GMT format. But while displaying, I have to convert that GMT date to local date and show it on the page. Am really struggling a lot to get this.Thanks in advanceKannan
Kannan007a at 2007-7-9 6:22:45 > top of Java-index,Core,Core APIs...