I am trying to retrieve a set of values from database and assigned it to selectItem (Primefaces UI component). But it is not assigned rather it shows nullPointerException.
I have two view pages in my first page, am just having an command button to enter into a function for retrieving some data from DB, then i assigned those values to a selectITem(drop down) in another view page.
My first view page is
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head></h:head>
<h:body>
<h:form>
<p:panel header="autocomplete">
<h:panelGrid columns="2">
<p:commandButton value="Submit" action="#{receiveclass.retrieve}" ajax="false"> </p:commandButton>
</h:panelGrid>
</p:panel>
</h:form>
</h:body>
</html>
java code for the action is..
public String retrieve(){
FacesContext context = FacesContext.getCurrentInstance();
loginBean loginBean = (loginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", loginBean.class);
List<loginBean> retList=loginDao.retrieval();
loginBean.setRetList(retList);
return "success";
}
}
In the above code the value is set properly to retList after retrieving it from Database, Db part is written in loginDao.
loginBean is nothing but the managed bean of my next view page where i am having the selectItem tag, i called the setter of that selectItem with the retrieved list from DB.
the bean part is..
List<loginBean> retList;
public List<loginBean> getRetList() {
return retList;
}
public void setRetList(List<loginBean> retList) {
this.retList = retList;
In my next view page, i have assigned the value to selectItems as
<h:outputText value="Current City"></h:outputText>
<p:selectOneMenu style="width:150px" id="currentCity" value="#{loginBean.currentCity}" required="true" immediate="true" requiredMessage="Select your city" label="Country">
<f:selectItem value="#{loginBean.retList}"></f:selectItem>
</p:selectOneMenu>
After execution my console says...
SEVERE: Error Rendering View[/login.xhtml]
java.lang.NullPointerException
at org.apache.myfaces.shared_impl.renderkit.html.HtmlResponseWriterImpl.write(HtmlResponseWriterImpl.java:867)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeSelectItems(SelectOneMenuRenderer.java:282)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeInput(SelectOneMenuRenderer.java:96)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeMarkup(SelectOneMenuRenderer.java:75)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeEnd(SelectOneMenuRenderer.java:53)
I have tried several ways, but no improvement.