IllegalStateException using iterator?

Discussions

EJB programming & troubleshooting: IllegalStateException using iterator?

  1. IllegalStateException using iterator? (2 messages)

    Hi,

    I read in Prof EJB from Wrox that an IllegalStateException could be thrown if a CMR collection is changed implicitly while walking over the collection using an iterator.
    I have a One-To-Many relationship between Product and Component. Now when I walk through the collection components of entity Product and try to remove a component using iterator.remove() I get an IllegalStateException, too.
    What's going wrong here? Where do I get that exception, although I remove the entity from the collection explicitely?

    TIA
    Frank
  2. This might be a ConcurrentModificationException, but I don't really know because I'm not entirely sure if your exception is coming from the EJB container for something EJB specific, or something to do with the collection. This error would be thrown by the iterator.

    From the java docs
    "For example, it is not permssible for one thread to modify a Collection while another thread is iterating over it."

    Try splitting up the iterator loop from the remove call, if you still ge the same error then you know its not this.

  3. ..Maybe you forgot to call itr.next( ) before calling itr.remove( ) !!