Results 1 to 6 of 6
  1. #1
    diegoyj is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default Why i get an error?

    I have this code, if i remove from my program, it execute correctly.

    for (BaseCell c : cells) {
    if (c != null) {
    BaseCell progeny = c.replicate();
    if (progeny != null)
    newCells.add(progeny);
    }
    }

    cells.addAll(newCells);



    When i try to execute this part of code, i get this error:

    Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification( AbstractList.java:449)
    at java.util.AbstractList$Itr.next(AbstractList.java: 420)
    at Sim.run(Sim.java:127)
    at Sim.main(Sim.java:175)

    Some ideas? I think that the problem is when returning the object or something like that.

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Hmm.. Well, we don't know what BaseCell is and I'm anticipating newCells is a list and cells is an array of BaseCell. With those assumptions made, I'll try explaining what that code does in english and maybe then you can find the dilemma.

    Java Code:
    for (BaseCell c : cells) {
    if (c != null) {
    BaseCell progeny = c.replicate();
    if (progeny != null)
    newCells.add(progeny);
    }
    }
    This is a for-each loop. Element X will be stored in the BaseCell c until it loops again. If element X is not null, it will make a new BaseCell called progeny and it will have c.replicate(). And a list called newCells will add w/e progeny is, which should be the same as BaseCell c (replicate means copy).

    Java Code:
    cells.addAll(newCells);
    I have no idea what that is. If cells is an array (it should be), then addAll() method does nothing because there is no method called addAll() in Arrays.

    Arrays (Java Platform SE 6)
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    diegoyj is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks you, is all i need.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    addAll().

    That code as presented doesn't look to me like it should give you a ConcurrentModificationException, so I'm guessing we're missing the relevant bit of code.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,530
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    addAll().

    That code as presented doesn't look to me like it should give you a ConcurrentModificationException, so I'm guessing we're missing the relevant bit of code.
    True; my guess would be that the replicate() method removes the current cell from the cells list ... (just a guess).

    kind regards,

    Jos

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Which implies that the cell knows about the array of which it is a part...
    Hmm, not terribly pretty, but (assuming something isn't missing from that code) that would be the only possible candidate.

Similar Threads

  1. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  2. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  3. Replies: 3
    Last Post: 01-23-2009, 08:53 AM
  4. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 04:28 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
  •