Weird Concurrent Modification Exception

Discussion in 'Plugin Development' started by MCCoding, Oct 15, 2014.

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

    MCCoding

    For some reason I'm getting a Concurrent Modification Exception on this method anyone know on how or why?

    Code:java
    1. @EventHandler
    2. public void onNPCInteract(PlayerInteractEntityEvent event) {
    3. Player player = event.getPlayer();
    4. Iterator<NPC> iterator = npcs.iterator();
    5. while (iterator.hasNext()) {
    6. NPC npc = iterator.next(); //<--- throws exception
    7. if (event.getRightClicked() == npc.getEntity()) {
    8. Bukkit.getServer().getPluginManager().callEvent(new PlayerInteractNPCEvent(player, npc));
    9. }
    10. }
    11. }
     
  2. Offline

    mine-care

    if i recall correctly this exception is called from interator being modifyed at same time so here you can find a solutin :p
     
  3. MCCoding What is it you're iterating over? Do you modify it during iteration? Do you really need to iterate?
     
  4. Offline

    RawCode

    what about posting stacktrace?
     
  5. Offline

    MCCoding

    mine-care AdamQpzm the problem is that when I remove a NPC in the event I get ate exception but if I were to use a for loop I also get the exception, is there any way around this?
     
  6. Offline

    fireblast709

    MCCoding either you asynchronously modify the collections, or the PlayerInteractNPCEvent modifies the collection.
     
Thread Status:
Not open for further replies.

Share This Page