Results 1 to 6 of 6
  1. #1
    Miromancer is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Concurrent modification exception while using iterator

    Hello everyone,

    I have this annoying exception popping up from time to time and I have not been able to fix it. Here is the method from class Player.java

    I am not aware that I broke any iterator rules, so I am really staggered about this and would greatly appreciate any help.

    By the way, the collection is LinkedList<Integer>
    Java Code:
    Integer capitalContID = territories.get(loser.getCapital()).getContinentID();
    
    for (Iterator it = loser.getTerritoryIndices().iterator(); it.hasNext();)
    {
    Integer terriID = (Integer) it.next();
    Territory t = territories.get(terriID);
    if (t.getContinentID() == capitalContID /*&& !t.isCapital()*/)
    {
    t.setOwner(this);
    territoryIndices.add(terriID);
    t.setArmy(1);
    it.remove(); // here it crashes (but only sometimes, rarely)
    }
    }
    When I tried to store the items to be removed to a new collection and then call removeAll(), it started crashing on the line: Integer terriID = (Integer) it.next();
    But as in the example before hand, the crash occurs rarely.

    Thanks for any effort.

    Mirek

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Concurrent modification exception while using iterator

    Java Code:
    territoryIndices.add(terriID);
    you've modified the list that you are currently iterating over here.
    So when you next try and do something with the iterator it throws the error.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Miromancer is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: Concurrent modification exception while using iterator

    It would appear so, but unfortunately:

    territoryIndices != loser.territoryIndices

    To make it clear I should explain, that in this method a player(lets call him winner) conquered capital territory belonging to another player (lets call him loser).
    According to the rules of the game, when a player loses his capital territory, he also loses all the territories on the same continent. So I am not changing the collection.

    So we are iterating over loser's territories and some of them we add to the winner's territories.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Concurrent modification exception while using iterator

    In which case I would suggest checking whether your assumption is correct.
    Because (fromt he code supplied) that is the obvious place to look.
    See if territoryIndices == loser.territoryIndices (just like that), which will quickly tell you whether they are the same list.

    Or check whether this is being accessed on multiple threads, which is the other possibility I suppose, but that depends on code I can't see here.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Miromancer is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: Concurrent modification exception while using iterator

    Thank you very much, I myself began to suspect it, when you pointed out the line. I hope that is the case! I will investigate it further in the evening and post here, whether it worked.

  6. #6
    Miromancer is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: Concurrent modification exception while using iterator

    Yes, that was it. Thanks again!

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2011, 01:22 PM
  2. Replies: 0
    Last Post: 11-30-2011, 04:20 PM
  3. concurrent modification
    By mharsijamel in forum New To Java
    Replies: 1
    Last Post: 11-06-2011, 02:59 PM
  4. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  5. help with concurrent modification exception
    By jdgallag in forum New To Java
    Replies: 1
    Last Post: 11-30-2008, 09:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •