RuntimeException - ReadOnlyException

hello to everybody. I have a big problem! I have a List which i scan. During the scan i have to check if the object into the List has some parameters. What i'd like to do is remove the object if the parameters are not good and than continue to scan the list. How can i do?

i tried to do this:

while (it.hasNext())

{

BonusToken bt=(BonusToken)it.next();

if (bt.getBonusGotDate()!=null) {

bonusTokenList.remove(bt);

break;

}

}

but it doesn't work, it generates the Exception below:

java.lang.RuntimeException ... Read only persistent object.

Where is the mistake?I tried without "break" but it doesn't works anyway

Please help me!

[719 byte] By [ramboelmeioa] at [2007-11-27 5:42:15]
# 1

What should have break to do with it?

The important information is missing: what is the source of the iterator and how does the source get created? It seems, that it is a read only collection implementation and thus you cannot remove items. Further, you should use the iterators remove operation to prevent concurrent modification issues.

stefan.schulza at 2007-7-12 15:20:40 > top of Java-index,Core,Core APIs...
# 2
the source of iterator is the collection that i am scanning. "break" was just a trial but it is incorrect. I tried with the method remove() of the iterator but it doesn't work anyway. It is the first time i see a ReadOnly collection....
ramboelmeioa at 2007-7-12 15:20:40 > top of Java-index,Core,Core APIs...
# 3
i forgot to say that i create the collection like below: List bonusTokenList=new ArrayList();
ramboelmeioa at 2007-7-12 15:20:40 > top of Java-index,Core,Core APIs...
# 4
I don't think so. I think you're getting it from a persistence layer somewhere like the exception is telling you.
ejpa at 2007-7-12 15:20:40 > top of Java-index,Core,Core APIs...
# 5
that's true i am sorry, after i am calling a method and the collection change and became a persistent obj. I am just a student so i have not a lot of experience with this kind of obj.Now i am trying to do the method in an other way...Thank 's a lot for the moment.
ramboelmeioa at 2007-7-12 15:20:40 > top of Java-index,Core,Core APIs...
# 6
so it is true i am getting the collection from a persistent layer. How do they do in this cases?i need to scan the collection and remove the obj that i don't need.
ramboelmeioa at 2007-7-12 15:20:40 > top of Java-index,Core,Core APIs...
# 7

Copy it. Your code is flawed anyway, see below

List<BonusToken> list = new ArrayList<BonusToken>(persistentCollection);

Iterator<BonusToken> it = list.iterator();

while (it.hasNext())

{

BonusToken bt=(BonusToken)it.next();

if (bt.getBonusGotDate()!=null) {

it.remove();// => NB you must remove via the iterator when iterating

break;

}

Note that this removal only happens in memory, not in the persistent collection.

ejpa at 2007-7-12 15:20:40 > top of Java-index,Core,Core APIs...
# 8
So are you going to remove them from the persistent layer or do you need the reduced list locally only? In the first case, you will have to use the persistent layer API to remove the entries, in the latter, you should create your own list and add the ones you need.
stefan.schulza at 2007-7-12 15:20:40 > top of Java-index,Core,Core APIs...
# 9
probably the best solution is remove locally only. I am going to create my own list as you suggest. Thank 's again to everybody and sorry for my little experience. :-(Message was edited by: ramboelmeio
ramboelmeioa at 2007-7-12 15:20:40 > top of Java-index,Core,Core APIs...
# 10
There is no need to be sorry for lack in experience (unless one pretended to have it). Asking questions usually is one way to gain from other's experience and adding to ones own.
stefan.schulza at 2007-7-12 15:20:40 > top of Java-index,Core,Core APIs...