java.util.ConcurrentModificationException

private int removeEzaSessionAttributes(HttpSession session){

Enumeration enu ;

enu = session.getAttributeNames();

String attibuteName ;

int counter= 0 ;

while(enu.hasMoreElements()){

attibuteName = enu.nextElement().toString();

if(attibuteName.startsWith("eza")){

session.removeAttribute(attibuteName);

counter++ ;

}

}

return counter;

}

//result >> Exception in removeEzaSessionAttributes() due to java.util.ConcurrentModificationException

How this Exception happens ?

anybody Pls help.

[609 byte] By [SebyJoseMathew] at [2007-9-30 11:03:43]
# 1

crossposted: http://forum.java.sun.com/thread.jsp?thread=531007&forum=31&message=2557561

You posted this half an hour after receiving three replies in the other thread. The first reply in that thread gives you the answer. I could spell it out for you, but won't bother as you crossposted. Go back to the other thread, read Kayaman's answer again, look at your code and think about it for a moment.

mattbunch at 2007-7-3 22:34:23 > top of Java-index,Administration Tools,Sun Connection...
# 2

The problem here is that, once you have an enumeration to operate on a kind of list, then you must use only the enumeration to do any kind of modification to that list.

But what you are doing is, you are trying to modify the list (by calling the removeAttribute() method) without using the enumeration.

So the Enumeration finds out that the list on which it is enumerating is being modified without its use. So it throws ConcurrentModificationException.

Solution:

Well, I don't know exactly.

Only hint that I can give you is that,

before calling

session.removeAttribute(attibuteName);

you should remove that object "attributeName" from the Enumeration.

I don't know exactly how to do it.

But if you use Iterator, then you have the advantage of calling the remove() method on the iterator before the statement

session.removeAttribute(attibuteName);

RohanJF at 2007-7-3 22:34:23 > top of Java-index,Administration Tools,Sun Connection...