[SOLVED] Caused by: java.util.ConcurrentModificationException

Discussion in 'Plugin Development' started by gregroberti, Jul 3, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    gregroberti

    Hey guys, I'm pretty stumped by this bug that I've just introduced to my plugin. It would appear that I have more than one thread running, however, I don't believe that I do. Is there any nice way to double check? I might mention that I'm using the Bukkit's scheduler in some parts of the code but I make it a point to only use scheduleSyncDelayedTask() and scheduleSyncRepeatingTask(), neither of which should be spawning off any threads.. right?

    My bug surfaces when I'm trying to do some cleanup where I iterate through an array list of Sign blocks and one by one try to remove them from my list and set them back to air.

    Code:
    for(Block signBlock : signBlocks)
    {
      signBlock.setType(Material.AIR);
      signBlocks.remove(signBlock);
    }
    Any help would be greatly appreciated because this bug is driving me nuts trying to figure it out..

    Code:
    13:50:04 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'leavebattle' in plugin SuitUp v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
            at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
            at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:567)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.util.ConcurrentModificationException
            at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
            at java.util.ArrayList$Itr.next(Unknown Source)
            at me.gregroberti.SuitUp.Battle.removeSigns(Battle.java:1077)
            at me.gregroberti.SuitUp.Battle.stopBattle(Battle.java:181)
            at me.gregroberti.SuitUp.BattleManager.stopBattle(BattleManager.java:652)
            at me.gregroberti.SuitUp.BattleManager.removePlayer(BattleManager.java:414)
            at me.gregroberti.SuitUp.BattleManager.leaveBattle(BattleManager.java:768)
            at me.gregroberti.SuitUp.SuitUp.onCommand(SuitUp.java:93)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
            ... 12 more
     
  2. if signBlocks is modifed while in use, like looping unless whit an iretator, then it does that,
    fixed code:
    Code:java
    1. //ferrybig start
    2. Iterator<Block> itr = signBlocks.iterator();
    3. while(itr.hasNext())
    4. {Block signBlock = itr.next
    5. //ferrybig end
    6. signBlock.setType(Material.AIR);
    7. //ferrybig start
    8. itr.remove();
    9. }
    10. //ferrybig end
     
    Mudsquisher likes this.
  3. Offline

    Milkywayz

    This happens when you work with threads. I believe you can just use a try and catch, because in my experience the intended action happened but also with an error. You can also try to use :
    Code:
    synchronized(signBlocks){
    for(Block signBlock : signBlocks)
    {
      signBlock.setType(Material.AIR);
      signBlocks.remove(signBlock);
    }
    }
    Just some suggestions.
     
  4. Offline

    gregroberti

    Thank you so much, you have no idea :)

    Just so I can understand this a bit better, is the arraylist signBlocks being changed somewhere else in the code (in another thread maybe?) while I'm trying to reset these blocks back to air? I guess I'll have to go back and fix a bunch of my other loops now too.

    Thanks again!
     
  5. Offline

    nisovin

    This doesn't have anything to do with threading, ferrybig has the right answer. You can't remove items from a list while iterating over it (unless you use an Iterator).
     
    gregroberti likes this.
  6. Offline

    gregroberti

    Got it, thanks guys!
     
Thread Status:
Not open for further replies.

Share This Page