Solved ConcurrentModificationException

Discussion in 'Plugin Development' started by TopTobster5, May 30, 2014.

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

    TopTobster5

    So here is my code:
    Code:
        public void run() {
            if (timerseconds <= 0) {
                try {
                    for (String pl : game.getPlayers()) {
                        game.removePlayer(game.getPlayer(pl));
                    }
                } catch (ConcurrentModificationException e) {
                }
                this.cancel();
            } else if (timerseconds % 60 == 0) {
                if (timer == 1) {
                    game.tellPlayers("There is 1 minute left!");
                } else {
                    game.tellPlayers("There are " + timer + "minutes left!");
                }
            }
            timerseconds--;
        }
    Line 4 throws a ConcurrentModificationException and I understand what it is, and why what I have done it wrong. However:
    1. The code I have currently works, my try/catch means that the error is not spat out into the console, and the code actually does run. If this runs without problems, why does it throw it in the first place?

    2. What can I do to fix it if what I'm doing shouldn't be done? Can I simply remakes the method in the same thread? Is there a way to use the same method?

    I am open to suggestions and I am thankful for any response I get. Again, I know what I have done wrong, and I know that I probably shouldn't fix it the way I have, I'm just experimenting with it a bit.
     
  2. Offline

    Not2EXceL

    Looks like you're removing a Collection's member while enumerating the Collection. Simple fix, enumerate a copy of the Collection, and remove the member from the original Collection.

    Note: there's several ways of avoiding what you're doing, I just suggested one that's not overly difficult for your situation
     
  3. Offline

    TopTobster5

    Not2EXceL I'm not sure I understand what you mean. Would you mind explaining it to me like I'm an idiot? It is mainly the 'enumerate' part, I understand what a collection and a member is.
     
  4. Offline

    ZeusAllMighty11

    Code:
    List<Object>toRemove = new ArrayList<>();
     
    for(Object obj : myList)
        toRemove.add(obj);
     
     
    myList.removeAll(toRemove);
    
    Simple way to do it
     
  5. Offline

    TopTobster5

    ZeusAllMighty11 So I just need to make a clone of the list before I delete things from the old list?
     
  6. Offline

    Not2EXceL

    Enumeration is looping. aka 'for (String s : List<String>)'.

    Yeah, that's pretty much it. You can't modify a list while looping over it
     
  7. Offline

    TopTobster5

  8. Offline

    ZeusAllMighty11


    I would say that 'iterating' is a better word
     
    WinX64 and mazentheamazin like this.
  9. Offline

    rsod

    Use interator.
     
  10. Offline

    Not2EXceL

    fak must have been real tired last night. considering I was actually attempting to be nice in my posts.

    enumerating, would have been more appropriate derp
     
Thread Status:
Not open for further replies.

Share This Page