7 Replies Latest reply on Aug 10, 2009 3:48 PM by jkronegg

    GUI: SelectOneMenu not displayed => NullPointerException

    acgrama

      Hello all,


      I am learning Seam and I have some problems creating a test drop down list. In particular, I get a null pointer exception. What I did is just a small test, as follows (only relevant snippets posted):


      The .xhtml




      <h:selectOneMenu value="#{ping.name}">
           <s:selectItems value="#{ping.yearList}" />
      </h:selectOneMenu>





      The bean (has @Name(ping) annotation):


           private List<SelectItem> myYearList = new ArrayList<SelectItem>();
      
           public List<SelectItem> getYearList() {
                populateYears();
                return myYearList;
           }
           
           public void setYearList(List<SelectItem> theYearList) {
                myYearList = theYearList;
           }
           
            public void populateYears(){
                 List<SelectItem> selectItems = new ArrayList<SelectItem>();
                 for (int i=0; i<=10; i++){
                      selectItems.add(new SelectItem(String.valueOf(i), String.valueOf(i)));
              }
                 setYearList(selectItems);
            }





      And the bean class extends an interface which has the getter/setter methods for the year list.


      When running the app, I get:



      java.lang.NullPointerException
           at com.sun.faces.context.RequestMap.put(ExternalContextImpl.java:1066)
           at org.jboss.seam.ui.component.UISelectItems$ContextualSelectItem.setup(UISelectItems.java:73)
           at org.jboss.seam.ui.component.UISelectItems$ContextualSelectItem.create(UISelectItems.java:90)
           at org.jboss.seam.ui.component.UISelectItems.asSelectItems(UISelectItems.java:195)
           at org.jboss.seam.ui.component.UISelectItems.getValue(UISelectItems.java:166)
      .....




           


      My environment:
      JBoss 4.2.2
      Seam 2.1.2
      Java 6


      Thank you in advance!
      --Cristina.

        • 1. Re: GUI: SelectOneMenu not displayed => NullPointerException
          acgrama

          Ack, bad formatting, sorry! Trying again here to make it easier to read:


          The .xhtml





          <h:selectOneMenu value="#{ping.name}">
           <s:selectItems value="#{ping.yearList}" />
          </h:selectOneMenu>






          The bean (has @Name(ping) annotation):


           private List<SelectItem> myYearList = new ArrayList<SelectItem>();
          
           public List<SelectItem> getYearList() {
            populateYears();
            return myYearList;
           }
             public void setYearList(List<SelectItem> theYearList) {
            myYearList = theYearList;
           }
              public void populateYears(){
             List<SelectItem> selectItems = new ArrayList<SelectItem>();
             for (int i=0; i<=10; i++){
              selectItems.add(new SelectItem(String.valueOf(i), String.valueOf(i)));
                  }
             setYearList(selectItems);
            }





          And the bean class extends an interface which has the getter/setter methods for the year list.


          When running the app, I get:



          java.lang.NullPointerException
           at com.sun.faces.context.RequestMap.put(ExternalContextImpl.java:1066)
           at org.jboss.seam.ui.component.UISelectItems$ContextualSelectItem.setup(UISelectItems.java:73)
           at org.jboss.seam.ui.component.UISelectItems$ContextualSelectItem.create(UISelectItems.java:90)
           at org.jboss.seam.ui.component.UISelectItems.asSelectItems(UISelectItems.java:195)
           at org.jboss.seam.ui.component.UISelectItems.getValue(UISelectItems.java:166)
          .....



          • 2. Re: GUI: SelectOneMenu not displayed => NullPointerException
            digdas.seam.digdas.nl

            Do you have a getName in ping?


            The SelectItem class, is that one of your own?
            Then you should add convertEntity (see below)




            <h:selectOneMenu value="#{ping.name}">
             <s:selectItems value="#{ping.yearList}" />
             <s:convertEntity/>
            </h:selectOneMenu>



            and check if the following block does not return null...




            public List<SelectItem> getYearList() {
              populateYears();
              return myYearList;
             }



            Hope you get some clues here...


            • 3. Re: GUI: SelectOneMenu not displayed => NullPointerException
              acgrama

              Thank you for your answer, my replies below!


              - Yes, I have a getName() in the bean and it works, tested it with an h:outputText
              - SelectItem is javax.faces.model.SelectItem - I understood (maybe incorrectly) from various sources that drop down items must be instances of this SelectItem class?
              - the getYearList() method cannot return null because it's initialised with dummy test values right in the bean (in the populateYears() method invoked by the getter). Even initialising it in the getter does not solve the problem.


              I can give a bit more detail now about the exception. I inserted for debugging purposes some code in the getYearList() method which prints out the array. Judging by the outputs in the server console, the array is displayed once, then the error appears, then it is displayed twice more.


              Also, from the server console, the lines above the NPE (which I couldn't see in the browser):



              13:43:21,351 ERROR [STDERR] 16.06.2009 13:43:21 com.sun.facelets.FaceletViewHandler handleRenderException
              SCHWERWIEGEND: Error Rendering View[/ping.xhtml]
              java.lang.NullPointerException




              ...


              I'm sure it's probably some basic beginner mistake that I'm doing, question is which mistake G

              • 4. Re: GUI: SelectOneMenu not displayed => NullPointerException
                acgrama

                Turns out it's a bad case of RTFM :-)


                The s:selectItems tag did not have the var and label attributes set; so I think when there was the need to hold the current object in the list during iteration, the container variable from var was null. It is highly likely that my understanding of the mechanism is still a bit sketchy, but at any rate, got rid of the error.


                Thank you anyway, Aart!

                • 5. Re: GUI: SelectOneMenu not displayed => NullPointerException
                  zhangxiubo

                  I'm not sure if I understood your solution correctly, but I think it should look like this:



                  <h:selectOneMenu value="#{ping.name}">
                   <s:selectItems value="#{ping.years}" var="year" label="#{year.label}" 
                      itemValue="#{year.name}"/>
                   <s:convertEntity/>
                  </h:selectOneMenu>




                  years: List<Year>


                  year.label: the label you want to dispaly in the drop down menu


                  year.name: should be the value you want to pass back to ping.name

                  • 6. Re: GUI: SelectOneMenu not displayed => NullPointerException
                    acgrama

                    Thank you for your comment and I apologise for the delay in answering! This addresses a new issue I'm having trouble with, so it's very welcome.


                    I am having trouble retrieving the value from the GUI into a simple Java type (String or Long or whatever). In particular, what I notice is that I can only pass values from the GUI to the backing bean via a customised object (i.e., if the backing bean has a String field name and a field year of type Year which has itself a String field name, I can only read the contents of the textfield in year.name).
                    Otherwise, I get all kinds of errors (Error reading type X on class... (even though it has appropriate getters/setters and the methods were declared in the Ping interface).


                    To hopefully make it more clear, details of the only configuration that works for me below (only relevant snippets, was modified/cleaned up after the previous posts):



                    Backing bean:


                    @Stateless
                    @Name("ping")
                    public class PingBean implements Ping
                    {
                         @In(required = false)
                         private Year year; // + getter and setter
                         ...
                    }
                    



                    Year object:


                    @Entity
                    @Name("year")
                    @Table(name = "year", catalog = "reg")
                    public class Year implements java.io.Serializable {
                    
                         private String name; // + getter&setter
                         private long yval; // + getter&setter
                         ...
                    }





                    Automatically generated by seam generate for the year table:



                    @Name("yearList")
                    public class YearList extends EntityQuery<Year> {
                         private static final String EJBQL = "select year from Year year";
                    
                         private static final String[] RESTRICTIONS = {"lower(year.name) like lower(concat(#{yearList.year.name},'%'))",};
                    
                         private Year year = new Year();
                    
                         public YearList() {
                              setEjbql(EJBQL);
                              setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
                              setMaxResults(25);
                         }
                    
                         public Year getYear() {
                              return year;
                         }
                    }




                    The xhtml:



                              <h:inputText id="yval" value="#{year.yval}" required="true"/>
                              <br/>
                              <h:selectOneMenu value="#{year.name}">
                                   <s:selectItems value="#{yearList}" var="year" label="#{year.name}" 
                                       itemValue="#{year.name}"/>
                              </h:selectOneMenu>




                    My question: is this the only way to do it? If not, is it best practice? Or what am I doing wrong?


                    Thanks a lot again.

                    • 7. Re: GUI: SelectOneMenu not displayed => NullPointerException
                      jkronegg

                      I think that throwing a NPE is a bug for that missing var attribute, so I openened JBSEAM-4347