I'm using JSF 2 (Mojarra 2.2.11) with WildFly9.0.1.
I have 3 selectOneMenu
s as seen below. The ajax events work fine loading data to the next one using omnifaces converter
.
Problem: even if the third "Header" selectOneMenu
is populated, on h:commandButton
action I can't get the Header Object giving a Null in the backing bean.
I'm able to get the Module Object calling saveNewHeader
method but saveNewQuestion
crashes when I'm trying to access the Header Object.
From the reference: Retrieving the value of the selected item in a SelectOneMenu List, I am suspecting a queue processing issue of JSF?
How can I solve/manage it? Thank you for your time.
Console:
12:37:17,583 FATAL [javax.enterprise.resource.webcontainer.jsf.context] (default task-1) JSF1073: javax.faces.FacesException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=, Message=#{questionBean.saveNewQuestion(questionBean.header)}: java.lang.NullPointerException
...
xhtml:
<ui:composition xmlns=...>
<h:head></h:head>
<h:body>
<h:form id="frmQuestion" >
<h4>Add Question to Header</h4>
<h:panelGrid columns="2">
<h:outputLabel value="ProductType: " />
<h:selectOneMenu value="#{questionBean.productType}" converter="omnifaces.SelectItemsConverter">
<f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
<f:selectItems value="#{questionBean.productTypes}" var="productType"
itemValue="#{productType}" itemLabel="#{productType.type}" />
<f:ajax listener="#{questionBean.loadModules()}" render="moduleId2" />
</h:selectOneMenu>
<h:outputLabel value="Module: " />
<h:selectOneMenu id="moduleId2" value="#{questionBean.module}" converter="omnifaces.SelectItemsConverter">
<f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
<f:selectItems value="#{questionBean.modules}" var="module"
itemValue="#{module}" itemLabel="#{module.moduleName}" />
<f:ajax listener="#{questionBean.loadHeaders()}" render="headerId2" />
</h:selectOneMenu>
<h:outputLabel value="Header Name: " />
<h:selectOneMenu id="headerId2" value="#{questionBean.header}" converter="omnifaces.SelectItemsConverter">
<f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
<f:selectItems value="#{questionBean.headers}" var="header"
itemValue="#{header}" itemLabel="#{header.name}" />
</h:selectOneMenu>
</h:panelGrid>
<h:commandButton action="#{questionBean.saveNewQuestion(questionBean.header)}" value="Add New Question"
/>
...
QuestionBean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class QuestionBean implements Serializable {
private List<Header> headers; // getter & setter
private Header header; // getter & setter
private List<Module> modules; // getter & setter
private Module module; // getter & setter
@PostConstruct
public void init(){
this.productTypes = publicService.getAllproductTypes();
}
public void saveNewHeader(Module module)
{
System.out.println(module.getModuleName()); // works fine
}
public void saveNewQuestion(Header header){
System.out.println(header.getHeaderName()); // crashes here
}
UPDATE (after BalusC comment)
It looks like a the bean behaves like a @RequestScoped
: how can this happen?