jsf - java.util.ConcurrentModificationException with c:forEach to loop and f:ajax to add items to ArrayList -
yes, question concurrentmodificationexception.
my problem:
- i loopping through arraylist using
<c:foreach>
- i adding value arraylist ajax call
- i getting java.util.concurrentmodificationexception.
here block of xhtml:
<h:form id="frmquestion" styleclass="form-horizontal"> <h:panelgroup layout="block" id="foreachanswer"> <div> <c:foreach items="#{questionbean.question.answers}" var="answer" varstatus="loopanswer"> ... </c:foreach> </div> <h:commandlink immediate="true" styleclass="list-group-item list-group-item-success text-center" action="#{questionbean.addanswer}"> <f:ajax execute="@all" render="frmquestion:foreachanswer" onevent="ajaxanswer" /> <i class="fa fa-plus"></i> adicionar pergunta </h:commandlink> </h:panelgroup> </h:form>
the @managedbean:
@managedbean @sessionscoped public class questionbean { private question question = new question(); public questionbean() { } public void addanswer() { this.question.addanswer(); } ... }
the question class:
@entity @table public class question implements serializable { @id @sequencegenerator(name="question_pk_sequence", sequencename="seq_rs_question") @generatedvalue(strategy=generationtype.auto, generator="question_pk_sequence") private int qst_id; private int qst_type; @onetomany private list<answer> answers = new arraylist<>(); public void addanswer() { if (this.answers == null) { this.answers = new arraylist<>(); } answer p = new answer(); p.setorder(this.answers.size()+1); if (this.qst_type == 1 || this.qst_type == 2) { p.settype(qst_type); } this.answers.add(p); //this raises exception } ... }
i searched solutions none of answers found facing problems <c:foreach>
. code working before , don't know did messed all. looked code , didn't see other place answers
list navigated.
is possible <c:foreach>
keeps looping through collection when ajax called? if is, why?
the best solution found change question.answers
arraylist
copyonwritearraylist
, can't because use collections.sort(answers);
in part of question class , throws exception when using copyonwritearraylist
.
i accepting solutions make collections.sort()
work copyonwritearraylist
too, don't have experience java (as it's obvious because think asking noob question).
edit:
stack trace:
java.util.concurrentmodificationexception @ java.util.arraylist$itr.checkforcomodification(unknown source) @ java.util.arraylist$itr.next(unknown source) @ com.sun.faces.facelets.tag.jstl.core.foreachhandler.apply(foreachhandler.java:193) @ javax.faces.view.facelets.compositefacelethandler.apply(compositefacelethandler.java:95) @ javax.faces.view.facelets.delegatingmetataghandler.applynexthandler(delegatingmetataghandler.java:137) @ com.sun.faces.facelets.tag.jsf.componenttaghandlerdelegateimpl.apply(componenttaghandlerdelegateimpl.java:190) @ javax.faces.view.facelets.delegatingmetataghandler.apply(delegatingmetataghandler.java:120) @ javax.faces.view.facelets.compositefacelethandler.apply(compositefacelethandler.java:95) @ javax.faces.view.facelets.delegatingmetataghandler.applynexthandler(delegatingmetataghandler.java:137) @ com.sun.faces.facelets.tag.jsf.componenttaghandlerdelegateimpl.apply(componenttaghandlerdelegateimpl.java:190) @ javax.faces.view.facelets.delegatingmetataghandler.apply(delegatingmetataghandler.java:120) @ com.sun.faces.facelets.tag.jsf.core.viewhandler.apply(viewhandler.java:212) @ javax.faces.view.facelets.compositefacelethandler.apply(compositefacelethandler.java:95) @ com.sun.faces.facelets.tag.ui.definehandler.applydefinition(definehandler.java:106) @ com.sun.faces.facelets.tag.ui.compositionhandler.apply(compositionhandler.java:201) @ com.sun.faces.facelets.impl.defaultfaceletcontext$templatemanager.apply(defaultfaceletcontext.java:395) @ com.sun.faces.facelets.impl.defaultfaceletcontext.includedefinition(defaultfaceletcontext.java:366) @ com.sun.faces.facelets.tag.ui.inserthandler.apply(inserthandler.java:111) @ javax.faces.view.facelets.compositefacelethandler.apply(compositefacelethandler.java:95) @ javax.faces.view.facelets.delegatingmetataghandler.applynexthandler(delegatingmetataghandler.java:137) @ com.sun.faces.facelets.tag.jsf.componenttaghandlerdelegateimpl.apply(componenttaghandlerdelegateimpl.java:190) @ javax.faces.view.facelets.delegatingmetataghandler.apply(delegatingmetataghandler.java:120) @ javax.faces.view.facelets.compositefacelethandler.apply(compositefacelethandler.java:95) @ com.sun.faces.facelets.compiler.namespacehandler.apply(namespacehandler.java:93) @ com.sun.faces.facelets.compiler.encodinghandler.apply(encodinghandler.java:87) @ com.sun.faces.facelets.impl.defaultfacelet.include(defaultfacelet.java:312) @ com.sun.faces.facelets.impl.defaultfacelet.include(defaultfacelet.java:371) @ com.sun.faces.facelets.impl.defaultfacelet.include(defaultfacelet.java:350) @ com.sun.faces.facelets.impl.defaultfaceletcontext.includefacelet(defaultfaceletcontext.java:199) @ com.sun.faces.facelets.tag.ui.compositionhandler.apply(compositionhandler.java:169) @ com.sun.faces.facelets.compiler.namespacehandler.apply(namespacehandler.java:93) @ com.sun.faces.facelets.compiler.encodinghandler.apply(encodinghandler.java:87) @ com.sun.faces.facelets.impl.defaultfacelet.apply(defaultfacelet.java:161) @ com.sun.faces.application.view.faceletviewhandlingstrategy.buildview(faceletviewhandlingstrategy.java:972) @ com.sun.faces.lifecycle.renderresponsephase.execute(renderresponsephase.java:99) @ com.sun.faces.lifecycle.phase.dophase(phase.java:101) @ com.sun.faces.lifecycle.lifecycleimpl.render(lifecycleimpl.java:219) @ javax.faces.webapp.facesservlet.service(facesservlet.java:647) @ org.apache.catalina.core.standardwrapper.service(standardwrapper.java:1682) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:318) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:160) @ org.apache.catalina.core.standardpipeline.doinvoke(standardpipeline.java:734) @ org.apache.catalina.core.standardpipeline.invoke(standardpipeline.java:673) @ com.sun.enterprise.web.webpipeline.invoke(webpipeline.java:99) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:174) @ org.apache.catalina.connector.coyoteadapter.doservice(coyoteadapter.java:357) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:260) @ com.sun.enterprise.v3.services.impl.containermapper.service(containermapper.java:188) @ org.glassfish.grizzly.http.server.httphandler.runservice(httphandler.java:191) @ org.glassfish.grizzly.http.server.httphandler.dohandle(httphandler.java:168) @ org.glassfish.grizzly.http.server.httpserverfilter.handleread(httpserverfilter.java:189) @ org.glassfish.grizzly.filterchain.executorresolver$9.execute(executorresolver.java:119) @ org.glassfish.grizzly.filterchain.defaultfilterchain.executefilter(defaultfilterchain.java:288) @ org.glassfish.grizzly.filterchain.defaultfilterchain.executechainpart(defaultfilterchain.java:206) @ org.glassfish.grizzly.filterchain.defaultfilterchain.execute(defaultfilterchain.java:136) @ org.glassfish.grizzly.filterchain.defaultfilterchain.process(defaultfilterchain.java:114) @ org.glassfish.grizzly.processorexecutor.execute(processorexecutor.java:77) @ org.glassfish.grizzly.nio.transport.tcpniotransport.fireioevent(tcpniotransport.java:838) @ org.glassfish.grizzly.strategies.abstractiostrategy.fireioevent(abstractiostrategy.java:113) @ org.glassfish.grizzly.strategies.workerthreadiostrategy.run0(workerthreadiostrategy.java:115) @ org.glassfish.grizzly.strategies.workerthreadiostrategy.access$100(workerthreadiostrategy.java:55) @ org.glassfish.grizzly.strategies.workerthreadiostrategy$workerthreadrunnable.run(workerthreadiostrategy.java:135) @ org.glassfish.grizzly.threadpool.abstractthreadpool$worker.dowork(abstractthreadpool.java:564) @ org.glassfish.grizzly.threadpool.abstractthreadpool$worker.run(abstractthreadpool.java:544) @ java.lang.thread.run(unknown source)
thanks in advance.
Comments
Post a Comment