java.util.ConcurrentModificationException.

Discussion in 'Plugin Development' started by MetalGearDaner, Nov 21, 2014.

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

    MetalGearDaner

    Hello, my plugin throws this exception when the for... starts, can anybody help me please?

    This is the peace of code that throws it (there is a "public static List<String> strings = new ArrayList<String>();" in the class "Main") :
    Code:java
    1. for (String cadena : Main.strings) {
    2. int Iusuario = cadena.indexOf("'Usuario: ");
    3. int Iequipo = cadena.indexOf("Equipo: ");
    4. String usuario = cadena.substring(Iusuario + 10, Iequipo - 3);
    5. String equipo = cadena.substring(Iequipo + 8);
    6. if (target.getName().equalsIgnoreCase(usuario)) {
    7. sender.sendMessage(ChatColor.DARK_RED + target.getName() + ChatColor.RED + " ya está en el equipo " + ChatColor.DARK_RED + equipo);
    8. } else {
    9. String nameAndteam = name + " - " + "Equipo: " + team;
    10. Main.strings.add(nameAndteam);
    11. Main.p.getConfig().set("Players", Main.strings);
    12. Main.p.saveConfig();
    13. sender.sendMessage(ChatColor.AQUA + "Se ha añadido a " + ChatColor.DARK_AQUA + target.getName() + ChatColor.AQUA + " al equipo " + ChatColor.DARK_AQUA + team + ChatColor.AQUA + ".");
    14. }
    15. }


    This is the error report:
    Code:java
    1. Caused by: java.util.ConcurrentModificationException
    2. at java.util.ArrayList$Itr.checkForComodification(Unknown Source) ~[?:1.
    3. 8.0_25]
    4. at java.util.ArrayList$Itr.next(Unknown Source) ~[?:1.8.0_25]
    5. at command.CustomCommandHandler.command(CustomCommandHandler.java:149) ~
    6. [?:?]
    7. at main.Main.onCommand(Main.java:38) ~[?:?]
    8. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    9. ftbukkit-betaBuild3092.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    10. ... 13 more
     
  2. Offline

    Assist

    MetalGearDaner
    How about you Google search what the error is about?
     
  3. Offline

    MetalGearDaner

    I already done that, but I'm new on coding and I don't really understand how to fix the java.util.concurrentmodificationexception.
     
  4. Offline

    Assist

    MetalGearDaner
    You're modifying a collection while looping through its contents. In this case, the collection would be your Main.strings list.

    Either make an array copy of the list and loop through that, or use an Iterator.
     
Thread Status:
Not open for further replies.

Share This Page