0

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.

Karthikeyan
  • 757
  • 3
  • 11
  • 25

2 Answers2

1

There are 2 problems.

First, the <f:selectItem> represents a single select item, but yet you're passing a whole List of items through it. You should be using <f:selectItems> instead.

<f:selectItems value="#{loginBean.retList}" />

Second, PrimeFaces has a bug that it expects the select item label to be explicitly specified next to item value. So, you should specify both itemValue and itemLabel:

<f:selectItems value="#{loginBean.retList}" var="item" itemValue="#{item}" itemLabel="#{item}" />

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the reply, my problem is cleared now, and am really very much thankful to you, as because whenever am raising a query on stack overflow, am expecting ur reply, as expected ur reply is there within a hour. once again, thanks a lot.. – Karthikeyan Nov 04 '11 at 04:46
  • There is one more query, may i know, how to do this without using my first view page, i want to load the values of selectItems dynamically on the page load itself. – Karthikeyan Nov 04 '11 at 05:25
  • Do the loading job in bean's (post)constructor. – BalusC Nov 04 '11 at 11:27
  • I did the same, just see what happened, i have post it in another thread. – Karthikeyan Nov 04 '11 at 11:34
  • Everything works fine for me now, now i want to include something, in above code. loginbean.retlist just returns the list of cities to the selectItems, from the table city, it contains two fields, city_id and city name, I have displayed city name in the select items, but i want cityID to be its value, so i retrieved city ID in the separate list, and mapped it as `` . But the error i got is`Cannot convert [1, 2, 3, 4, 5, 6, 7] of type class java.util.ArrayList to int` – Karthikeyan Nov 05 '11 at 08:16
  • i can able to retrieve it as list only, but i want it to be save in another table as int, i got stuck here. – Karthikeyan Nov 05 '11 at 08:17
0

There are multiple basic problems.

  1. on jsf-side you need f:selectItems (plural) instead of f:selectItem (singular)
  2. Classnames should start upcase
  3. does loginbean extends jsf's selectitem? im not sure.
  4. beans should reference themselves using faces-context.xml configuration, not via elcontext.
Grim
  • 1,938
  • 10
  • 56
  • 123
  • Point 3 is not necessary since JSF 2.x. Points 2 and 4 are more "bad practice" problems, they do not cause technical problems. – BalusC Nov 03 '11 at 14:36
  • Code explains for self. The `p:selectOneMenu` is introduced in PrimeFaces 3.0 which in turn is compatible with JSF 2.0 or newer only. – BalusC Nov 03 '11 at 15:36