Dynamically add AjaxBehavior throws NullPointerException
Hi there!
I´m trying to add some ajax listeners using mojarra (tested with 2.2 and 2.3) and i receive a NullPointerException.
Using Myfaces everything works fine.
Exception:
java.lang.NullPointerException
at javax.faces.component.UIComponentBase.restoreBehaviors(UIComponentBase.java:2228)
at javax.faces.component.UIComponentBase.restoreBehaviorsState(UIComponentBase.java:2203)
at javax.faces.component.UIComponentBase.restoreState(UIComponentBase.java:1623)
at javax.faces.component.UIOutput.restoreState(UIOutput.java:286)
at javax.faces.component.UIInput.restoreState(UIInput.java:1422)
at com.sun.faces.application.view.FaceletPartialStateManagementStrategy$2.visit(FaceletPartialStateManagementStrategy.java:379)
at com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:151)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1689)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIForm.visitTree(UIForm.java:371)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at com.sun.faces.application.view.FaceletPartialStateManagementStrategy.restoreView(FaceletPartialStateManagementStrategy.java:366)
at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:138)
at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:123)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:591)
at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:151)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:353)
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:199)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:123)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
....
Code:
<p:inputText id="txtTeste" value="#{user.desTeste}">
<p:ajax
event="change"
listener="#{user.listener}" />
</p:inputText>
<p:commandButton
action="#{user.actionCustomizar}"
value="Adiciona"
ajax="false" />
public void actionCustomizar() {
ExpressionFactory factory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
ELContext elContext = FacesContext.getCurrentInstance().getELContext();
MethodExpression me = factory.createMethodExpression(elContext, "#{user.listenerKeyUp}", null, new Class<?>[] { BehaviorEvent.class });
AjaxBehavior ajaxBehavior = (AjaxBehavior) FacesContext.getCurrentInstance().getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
ajaxBehavior.setProcess("@this");
ajaxBehavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(me, me));
ajaxBehavior.setUpdate("@this");
InputText c = (InputText) this.findComponent("txtTeste");
c.addClientBehavior("keyup", ajaxBehavior);
}
And yes, AjaxBehavior is a primefaces class, but using myfaces this code works, so after a lot of tries my conclusion is that there is a bug on this scenario.
Tested with a lot of different servers (tomee, wildfly 10, 11 and 12) and the result is always the same (on mojarra the code doesnt work)
Any help will be appreciated.
Just to add:
Same problem seems to happen with other people:
https://stackoverflow.com/questions/41424132/not-able-to-add-new-ajax-behavior-to-primefaces-datatable-dynamically-when-it-al