Solved Iterator Error

Discussion in 'Plugin Development' started by mrgreen33gamer, Aug 28, 2015.

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

    mrgreen33gamer

    Hi there,

    I'm having a HUGE issue with iterating through my HashMap from another class. The Keys and Values are adding correctly, but when I try to iterate through it, it doesn't seem to work. I get an error on the 'for' statement.

    Here is the iterate method:

    Code:java
    1.  
    2. for (Map.Entry<String, Integer> entry : kcRef.getStarterMap().entrySet()) {
    3. String name = entry.getKey();
    4. int longValue = entry.getValue();
    5. if(longValue <= 0){
    6. kcRef.getStarterMap().remove(name);
    7. }else{
    8. kcRef.getStarterMap().put(name, longValue - 1);
    9. }
    10. }
    11.  


    I got a NULL error on this part:
    Code:java
    1. for (Map.Entry<String, Integer> entry : kcRef.getStarterMap().entrySet()) {

    So I really don't know what to make of it. Here is the error text:

    Code:
    28.08 21:57:41 [Server] INFO ... 15 more
    28.08 21:57:41 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at com.mrgreen33gamer.server.Main.onCommand(Main.java:541) ~[?:?]
    28.08 21:57:41 [Server] INFO at java.util.HashMap$EntryIterator.next(HashMap.java:1461) ~[?:1.8.0_11]
    28.08 21:57:41 [Server] INFO at java.util.HashMap$EntryIterator.next(HashMap.java:1463) ~[?:1.8.0_11]
    28.08 21:57:41 [Server] INFO at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429) ~[?:1.8.0_11]
    28.08 21:57:41 [Server] INFO Caused by: java.util.ConcurrentModificationException
    28.08 21:57:41 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_11]
    28.08 21:57:41 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_11]
    28.08 21:57:41 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_11]
    28.08 21:57:41 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:970) [custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1135) [custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:640) ~[custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[custom.jar:git-Spigot-6d16e64-b105298]
    28.08 21:57:41 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin XeneonPvP v1.1.0
    28.08 21:57:41 [Server] Server thread/ERROR null
    
    Code:
    com.mrgreen33gamer.server.Main.onCommand(Main.java:541)
    is this line:
    Code:java
    1. for (Map.Entry<String, Integer> entry : kcRef.getStarterMap().entrySet()) {


    I have no ideas but I did spend over 3 hours trying to resolve this issue, and nothing so far. Not even Google and assist me.

    Anyway, if you got any advice/questions/help on this, please reply :D!

    ~mrgreen33gamer | Josh
     
  2. Offline

    adam753

    You seem to have cut out the part of the error message saying what type of exception this is? Is it a NullPointerException?
     
  3. Offline

    mrgreen33gamer

    @adam752

    No NullPointerException, if that was the case I would've figured this out easier. The HashMap contains all it's values and keys. The entire error code is in post.
     
  4. Offline

    meguy26

    Mm.... Concurrent modification...
    Basically you are trying to edit the map in two places, at the same time.

    In not on my computer at the moment, but perhaps you could try iterating over the keys of the map instead.

    @mrgreen33gamer
     
  5. Offline

    sionzee

    Isn't kcRef null?
    Isn't kcRef.getStarterMap() null?

    ConcurrentModificationException you can receive if you foreaching empty list/map.
    Make yourself sure if size > 0 or use Iterator.

    Then repair your code formatting.
     
  6. Offline

    teej107

    I don't think so.
     
  7. Offline

    finalblade1234

    @mrgreen33gamer This happens when your using a foreach and you add something to the map/list your using, try changing your HashMap to a ConcurrentHashMap
     
  8. Offline

    teej107

    Try to not
    and just don't
     
  9. Offline

    mrgreen33gamer

    @finalblade1234 Your method seemed to work! Thanks :D!!!!

    Also, thanks to all of those who have replied :D!
     
Thread Status:
Not open for further replies.

Share This Page