'for' function not working?

Discussion in 'Plugin Development' started by SoS_Dylan, Feb 16, 2013.

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

    SoS_Dylan

    Code:
    Code:java
    1. for (String credit : credits) {
    2. if (credit.startsWith(player.getName() + ":")) {
    3. amm = credit.replace(player.getName() + ":", "");
    4. credits.remove(credit);
    5. }
    6. }

    Error:
    Code:
    2013-02-17 12:23:47 [INFO] SoS_Dylan issued server command: /buy
    2013-02-17 12:23:47 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'buy' in plugin PvP v1.0
    >      at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    >      at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    >      at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer.java:514)
    >      at net.minecraft.server.v1_4_6.PlayerConnection.handleCommand(PlayerConnection.java:979)
    >      at net.minecraft.server.v1_4_6.PlayerConnection.chat(PlayerConnection.java:897)
    >      at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:852)
    >      at net.minecraft.server.v1_4_6.Packet3Chat.handle(Packet3Chat.java:44)
    >      at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
    >      at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:112)
    >      at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
    >      at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30)
    >      at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:598)
    >      at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
    >      at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
    >      at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
    >      at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.util.ConcurrentModificationException
    >      at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
    >      at java.util.AbstractList$Itr.next(AbstractList.java:343)
    >      at me.SoSDylan.PvP.Main.onCommand(Main.java:352)
    >      at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    >      ... 15 more
    >
    
     
  2. Offline

    chasechocolate

  3. Offline

    Tirelessly

    You're removing from the list while iterating through it.
     
  4. You can use an Iterator to remove elements while looping....

    Code:
    Iterator<String> iterator = credits.iterator();
    String credit;
    
    while(iterator.hasNext())
    {
        credit = iterator.next(); // get next element and store it ! because every time you call .next() it will give you the next one.
    
        if( ... whatever)
            iterator.remove(); // remove current element
    }
     
Thread Status:
Not open for further replies.

Share This Page