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)