简体   繁体   中英

Java applet throws error in remove object from Iterator array

Welcome, i am programming a simple RPG game in java applet. It is getting more and more complicated and many errors are thrown to my face. Today i have problem with .remove(); method.

Exception in thread "AWT-EventQueue-1" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.remove(Unknown Source)
    at rpg.main.paint(main.java:365)
    at rpg.main.update(main.java:331)
    at sun.awt.RepaintArea.updateComponent(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(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.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 Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    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 Source)
    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)

That's how error log looks like. Code of program:

main.java

As we can see error appears in paint methid in the end of program, but i dont know maybe it is caused by wrong initiation in the beginning of program?

public void initLocatables()
public void initLocatables2()

And i have to admit that they are called in main while loop in run method. These is place where error apears, and below in comment i have included previous method that has also thrown error, both in remove();

for (Iterator i = locatablesArray.listIterator(); i.hasNext();) {
               Locatable l = (Locatable) i.next(); 
               l.draw(g);
               i.remove();
            }
/*while(itrLocatables.hasNext())
{
    Locatable l = itrLocatables.next();
    l.draw(g);
    l.remove();
}*/

Thank you for any reply.

You cannot remove objects from a list/map while you are iterating them. That's the cause of the ConcurrentModificationException

       i.remove();

If you will draw all the Locatable elements, and then clear the collection, maybe you should draw them all first, and then clear the collection, instead of removing them on the fly.

for (Iterator i = locatablesArray.listIterator(); i.hasNext();) {
               Locatable l = (Locatable) i.next(); 
               l.draw(g);
}

// Clear everything from the list
locatablesArray.clear();

java.util.ConcurrentModificationException Another approach is to use a for loop through the locatablesArray starting at the end vs the beginning. That way any elements that are removed won't change the location of elements of the array that are yet to be scanned.

Maybe when you call repaint() in run() , paint() is queued to EDT and called as soon as possible; while iterating locatablesArray a call to initLocatables2() in run() is done another time and the re-init of locatablesArray in the same time the code of paint() will produce the error.
My advice is to separate init operation in a specific init section and call it once, the run your main loop.

Just another thing: why are you init a Iterator (class field itrLocatables ) in initLocatables2() ? Use iterator when needed if possible.

Hope to be clear, English is not my native language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM