Error on iterating through hashmap

Discussion in 'Plugin Development' started by Sorroko, Mar 3, 2012.

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

    Sorroko

    Hey all, so I am trying to iterate through a a hashmap and I have a strange error. It works when theres only one player in the HashMap but as soon as theres more than one I get this error:
    Code:
    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:461)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:818)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:778)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:761)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:112)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:554)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:452)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Caused by: java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
        at java.util.HashMap$EntryIterator.next(HashMap.java:834)
        at java.util.HashMap$EntryIterator.next(HashMap.java:832)
        at me.blocksparty.gungame.GunGame.GGStartMatch(GunGame.java:252)
        at me.blocksparty.gungame.GunGame.onCommand(GunGame.java:187)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    Here is my relevant code:
    Code:
    public HashMap<String, String> teams = new HashMap<String, String>();
    for(Map.Entry<String, String> entry: teams.entrySet()){
        Player player = getServer().getPlayer((String) entry.getKey());
        //run some more code
    }
     
  2. Are you by any chance removing stuff from the teams hashmap while in the iteration ? Because that's what the code is complaining about:
    Code:
    Caused by: java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
        at java.util.HashMap$EntryIterator.next(HashMap.java:834)
        at java.util.HashMap$EntryIterator.next(HashMap.java:832)
        at me.blocksparty.gungame.GunGame.GGStartMatch(GunGame.java:252)
     
  3. Offline

    Sorroko

    Ah thanks!
     
Thread Status:
Not open for further replies.

Share This Page