5

May Be it can be a duplicate question, I'm sorry for that. But my problem is not solving.

Below code gives me this exception (java.util.ConcurrentModificationException ) when execution reaches for 2nd time in foreach loop. Even i removed the arraylist object using iterator.

@RequestMapping("edit")
public ModelAndView editItemToInvoice(HttpSession session,@RequestParam("itemname")String itemname){

    ArrayList<InvoiceEntities> show=(ArrayList<InvoiceEntities>)session.getAttribute("invoices");

    if(show==null){
        show=new ArrayList<InvoiceEntities>();
    }

    ArrayList<InvoiceEntities> edit=new ArrayList<InvoiceEntities>();

    for(InvoiceEntities itemnam:show){
        if(itemnam.getItemName().equals(itemname)){
            int index=show.indexOf(itemnam);
            edit.add(show.get(index));

            Iterator<InvoiceEntities> iter = show.iterator();
            while(iter.hasNext()){
                InvoiceEntities getitem=iter.next();
                if(getitem.getItemName().equals(itemname)){
                    iter.remove();
                    //break;
                }
            }

        }
    }

    System.out.println(session.getAttribute("invoices"));

    ModelAndView model=new ModelAndView();
    session.setAttribute("invoices", show);
    model.addObject("editobj",edit);
    model.addObject("items",session.getAttribute("invoices"));
    model.setViewName("jsp/Invoice");

    return model;
}

Exception is java.util.ConcurrentModificationException

 SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with  
 path [/Invoice] threw exception [Request processing failed; nested 
 exception is java.util.ConcurrentModificationException] with root cause
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at   mine.Controllers.InvoiceContorller.editItemToInvoice(InvoiceContorller.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)

3 Answers3

4
for(InvoiceEntities itemnam:show)

creates behind the scene an iterator of show, then inside the for loop you create another iterator:

Iterator<InvoiceEntities> iter = show.iterator();

and use the second iterator to modify show by calling iter.remove(); while the first iterator is still iterating the same collection.

See the paragraph that starts with "For example" in the documentation of ConcurrentModificationException for more information.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
3

When you perform

iter.remove();

you are modifying the collection while iterating on it with a different iterator. ArrayList doesnt allow that.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
0

ArrayList is a Fail-Fast iterator collection. So, you should not modify it while iterating it. So, make sure it as you 're trying to remove an item from the list in your code.

Viswanath Donthi
  • 1,791
  • 1
  • 11
  • 12