2

Why is the below exception happening?

2012-08-28 11:41:59,183 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/TFO].[tfo]] (http-0.0.0.0-8080-9) Servlet.service() for servlet tfo threw exception: java.util.ConcurrentModificationException
            at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793) [:1.6.0_24]
            at java.util.HashMap$EntryIterator.next(HashMap.java:834) [:1.6.0_24]
            at java.util.HashMap$EntryIterator.next(HashMap.java:832) [:1.6.0_24]
            at net.sf.json.JSONObject._fromMap(JSONObject.java:1082) [:]
            at net.sf.json.JSONObject.fromObject(JSONObject.java:173) [:]
            at net.sf.json.JSONObject._processValue(JSONObject.java:2552) [:]
4
  • 2
    It would be difficult to tell without knowing what you tried to do. Commented Aug 29, 2012 at 12:25
  • 1
    possible duplicate of ConcurrentModificationException and a HashMap Commented Aug 29, 2012 at 12:26
  • I just accessed map from session and removed one object again i set it into session Commented Aug 29, 2012 at 12:27
  • Go through Below Link.. stackoverflow.com/questions/1066589/… Commented Jul 1, 2013 at 2:45

1 Answer 1

10

How have you tried to remove the object (key, value) in the map? If you used the for-each Construct and tried to remove it the Exception will be thrown even if your code executes in a single-threaded environment.

If you have iterated it like this:

for(Entry<String, Object> entry : session.entrySet()) {
   if (condition) {
      // throws a ConcurrentModificationException
      session.remove(entry.getKey());
   }
}

Then you should change it to that:

Iterator<Entry<String, Object>> it = session.entrySet().iteration;
while (it.hasNext) {
   Entry<String, Object> entry = it.next(); 
   if (condition) {
      it.remove(); // avoids a ConcurrentModificationException
   }
}

Similar discussed in the question Iterate through a HashMap

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.