[jdom-interest] From B7 to B8 - ConcurrentModificationException

Alex Rosen arosen at silverstream.com
Mon Mar 18 07:59:00 PST 2002


The List code has been completely rewritten and is much better now. Part of
the improvement is that the Lists (and therefore the Iterators) now see
changes to the backing Element when they happen. Your old code worked
because this was not true at the time (which was against the JDOM spec).
This means that the Iterators now throw ConcurrentModificationException,
just like ArrayList does:

"The iterators returned by this class's iterator and listIterator methods
are fail-fast: if list is structurally modified at any time after the
iterator is created, in any way except through the iterator's own remove or
add methods, the iterator will throw a ConcurrentModificationException.
Thus, in the face of concurrent modification, the iterator fails quickly and
cleanly, rather than risking arbitrary, non-deterministic behavior at an
undetermined time in the future."

The solution is to simply call i.remove() instead of
values.removeContent(e).

Alex

> -----Original Message-----
> From: jdom-interest-admin at jdom.org
> [mailto:jdom-interest-admin at jdom.org]On Behalf Of Bilton, Sasha
> Sent: Monday, March 18, 2002 9:51 AM
> To: jdom-interest at jdom.org
> Subject: [jdom-interest] From B7 to B8 -
> ConcurrentModificationException
>
>
> Hi,
>
> I've just converted from Beta 7 to Beta 8 RC1 and I've got
> the following
> exception coming up now...
>
> Exception in thread "main" java.util.ConcurrentModificationException
>         at
> org.jdom.ContentList$FilterListIterator.checkConcurrentModification(C
> ontentList.java:1206)
>         at
> org.jdom.ContentList$FilterListIterator.hasNext(ContentList.java:928)
> <snip>
>
> from this code
>
>   public void removeUnit(Unit unit)
>   {
>     String unitName = unit.getName();
>
>     Iterator i = getUnits().iterator();
>     while (i.hasNext())
>     {
>       Element e = (Element)i.next();
>       if(e.getTextTrim().equals(unitName))
>       {
>         values.removeContent(e);
>       }
>     }
>   }
>
> Where getUnits() returns a List of Elements, from the
> variable values that
> have a single text value.
>
> I guess that the error is occuring because I'm removing elements from
> 'values' at the same time as going through 'values' iterator.
> What's weird
> is that the error is occuring under B8 and not B7.
>
> Any hints on the best way to fix it and why it happening now
> would be a
> great help,
> thanks
>
> Sasha
>
>
> **********************************************************************
> Information in this email is confidential and may be privileged.
> It is intended for the addressee only. If you have received
> it in error,
> please notify the sender immediately and delete it from your system.
> You should not otherwise copy it, retransmit it or use or disclose its
> contents to anyone.
> Thank you for your co-operation.
> **********************************************************************
>
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr at yourhost.com




More information about the jdom-interest mailing list