ConcurrentModificationException thrown on iterator.remove()

Discussion in 'Plugin Development' started by ocomobock, Aug 10, 2013.

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

    ocomobock

    Code:java
    1.  
    2. Iterator<String>itr = grass2.iterator();
    3. while(itr.hasNext())
    4. {
    5. String f = itr.next();
    6. String[]c = f.split(":");
    7. int fx = Integer.parseInt(c[0]);int fy = Integer.parseInt(c[1]);int fz = Integer.parseInt(c[2]);
    8. p.getWorld().getBlockAt(new Location(p.getWorld(),fx,fy,fz)).setTypeId(2);
    9. grass2.remove(f);
    10. grass.add(f);
    11. itr.remove();
    12. }
    13.  


    It throws ConcurrentModificationException on itr.remove(). I tried to use a for loop before that looked like this:

    Show Spoiler
    Code:java
    1.  
    2. for(String f: grass2)
    3. {
    4. String[]c = f.split(":");
    5. int fx = Integer.parseInt(c[0]);
    6. int fy = Integer.parseInt(c[1]);
    7. int fz = Integer.parseInt(c[2]);
    8. p.getWorld().getBlockAt(new Location(p.getWorld(),fx,fy,fz)).setTypeId(2);
    9. grass2.remove(f);
    10. grass.add(f);
    11. }


    But I got the same exception on
    for(String f: grass2)

    Here's the entire method:

    Show Spoiler
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void kayaVictim(PlayerMoveEvent e)
    3. {
    4. final List<String>grass2 = new ArrayList<String>();
    5. final Player p = e.getPlayer();
    6. Location l = p.getLocation();
    7. World w = p.getWorld();
    8. int x = (int)l.getX();int y = (int)l.getY()-1;int z = (int)l.getZ();
    9. Iterator<String>itr1 = grass.iterator();
    10. while(itr1.hasNext())
    11. {
    12. String f = itr1.next();
    13. if ( !hasKit(p.getName(), Kit.KAYA)&&(
    14. coords( new Location(w,x+0,y,z)).equals(f)||
    15. coords( new Location(w,x+1,y,z)).equals(f)||
    16. coords( new Location(w,x+2,y,z)).equals(f)||
    17. coords( new Location(w,x-1,y,z)).equals(f)||
    18. coords( new Location(w,x-2,y,z)).equals(f)||
    19. coords( new Location(w,x+0,y,z+1)).equals(f)||
    20. coords( new Location(w,x+1,y,z+1)).equals(f)||
    21. coords( new Location(w,x+2,y,z+1)).equals(f)||
    22. coords( new Location(w,x-1,y,z+1)).equals(f)||
    23. coords( new Location(w,x-2,y,z+1)).equals(f)||
    24. coords( new Location(w,x+0,y,z+2)).equals(f)||
    25. coords( new Location(w,x+1,y,z+2)).equals(f)||
    26. coords( new Location(w,x+2,y,z+2)).equals(f)||
    27. coords( new Location(w,x-1,y,z+2)).equals(f)||
    28. coords( new Location(w,x-2,y,z+2)).equals(f)||
    29. coords( new Location(w,x+0,y,z)).equals(f)||
    30. coords( new Location(w,x+1,y,z)).equals(f)||
    31. coords( new Location(w,x+2,y,z)).equals(f)||
    32. coords( new Location(w,x-1,y,z)).equals(f)||
    33. coords( new Location(w,x-2,y,z)).equals(f)||
    34. coords( new Location(w,x+0,y,z-1)).equals(f)||
    35. coords( new Location(w,x+1,y,z-1)).equals(f)||
    36. coords( new Location(w,x+2,y,z-1)).equals(f)||
    37. coords( new Location(w,x-1,y,z-1)).equals(f)||
    38. coords( new Location(w,x-2,y,z-1)).equals(f)||
    39. coords( new Location(w,x+0,y,z-2)).equals(f)||
    40. coords( new Location(w,x+1,y,z-2)).equals(f)||
    41. coords( new Location(w,x+2,y,z-2)).equals(f)||
    42. coords( new Location(w,x-1,y,z-2)).equals(f)||
    43. coords( new Location(w,x-2,y,z-2)).equals(f)||
    44. //
    45. coords( new Location(w,x,y,z+0)).equals(f)||
    46. coords( new Location(w,x,y,z+1)).equals(f)||
    47. coords( new Location(w,x,y,z+2)).equals(f)||
    48. coords( new Location(w,x,y,z-1)).equals(f)||
    49. coords( new Location(w,x,y,z-2)).equals(f)||
    50. coords( new Location(w,x+1,y,z+0)).equals(f)||
    51. coords( new Location(w,x+1,y,z+1)).equals(f)||
    52. coords( new Location(w,x+1,y,z+2)).equals(f)||
    53. coords( new Location(w,x+1,y,z-1)).equals(f)||
    54. coords( new Location(w,x+1,y,z-2)).equals(f)||
    55. coords( new Location(w,x+2,y,z+0)).equals(f)||
    56. coords( new Location(w,x+2,y,z+1)).equals(f)||
    57. coords( new Location(w,x+2,y,z+2)).equals(f)||
    58. coords( new Location(w,x+2,y,z-1)).equals(f)||
    59. coords( new Location(w,x+2,y,z-2)).equals(f)||
    60. coords( new Location(w,x,y,z+0)).equals(f)||
    61. coords( new Location(w,x,y,z+1)).equals(f)||
    62. coords( new Location(w,x,y,z+2)).equals(f)||
    63. coords( new Location(w,x,y,z-1)).equals(f)||
    64. coords( new Location(w,x,y,z-2)).equals(f)||
    65. coords( new Location(w,x-1,y,z+0)).equals(f)||
    66. coords( new Location(w,x-1,y,z+1)).equals(f)||
    67. coords( new Location(w,x-1,y,z+2)).equals(f)||
    68. coords( new Location(w,x-1,y,z-1)).equals(f)||
    69. coords( new Location(w,x-1,y,z-2)).equals(f)||
    70. coords( new Location(w,x-2,y,z+0)).equals(f)||
    71. coords( new Location(w,x-2,y,z+1)).equals(f)||
    72. coords( new Location(w,x-2,y,z+2)).equals(f)||
    73. coords( new Location(w,x-2,y,z-1)).equals(f)||
    74. coords( new Location(w,x-2,y,z-2)).equals(f))
    75. )
    76. {
    77. String[]c = f.split(":");
    78. int fx = Integer.parseInt(c[0]);
    79. int fy = Integer.parseInt(c[1]);
    80. int fz = Integer.parseInt(c[2]);
    81. w.getBlockAt(new Location(w,fx,fy,fz)).setTypeId(0);
    82. grass.remove(f);
    83. grass2.add(f);
    84. itr1.remove();
    85. }
    86. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
    87. {
    88. public void run()
    89. {
    90. Iterator<String>itr = grass2.iterator();
    91. while(itr.hasNext())
    92. {
    93. String f = itr.next();
    94. String[]c = f.split(":");
    95. int fx = Integer.parseInt(c[0]);int fy = Integer.parseInt(c[1]);int fz = Integer.parseInt(c[2]);
    96. p.getWorld().getBlockAt(new Location(p.getWorld(),fx,fy,fz)).setTypeId(2);
    97. grass2.remove(f);
    98. grass.add(f);
    99. itr.remove();
    100. }
    101. }
    102. }, 60L);
    103. }
    104. }


    If you're wondering what this does, it makes it so whenever someone who doesn't have the Kaya kit will activate Kaya's fake grass blocks making them turn to air for a few seconds. This can be used for traps and stuff. Everything works fine except it throws that exception every time a fake grass block is activated. Any idea how I can avoid that?
     
  2. Offline

    LucasEmanuel

    ocomobock
    In the code you pasted, on line 9 you are still calling grass2.remove();
     
  3. Offline

    ocomobock

    What should I do about that then? I need that there for it to work.

    EDIT: Turns out I don't need to add that. It seems to be working, thanks
     
Thread Status:
Not open for further replies.

Share This Page