1 Reply Latest reply on Mar 7, 2009 7:50 PM by ellenzhao

    resultList scope question

    ellenzhao
      Seam version: 2.1.1 GA. Application server: JBoss 4.2.3 GA

      Say there are two entities called Song and Album having many-Song-to-one-Album association. After executing the "seam generate-ui", there are following java files:

      = SongHome.java
      = SongList.java
      = AlbumHome.java
      = AlbumList.java

      and the following autmatically generated xhtml files:

      = SongList.xhtml
      = SongEdit.xhtml
      = AlbumList.xhtml
      = AlbumEdit.xhtml

      and their corresponding page navigation descriptors.

      Now I want to implement a "Create Album" button in the SongList.xhtml page. The users can choose several songs in the SongList.xhtml page and then click the "create album" button. They should be landed at the AlbumEdit.xhtml with the chosen songs already wired to the newly created Album object.

      I did it like this:

      = Added a transient boolean field "selectedForAlbum" to the Song entity.
      = Added a column in the SongList.xhtml for song selection looking like this:
       
      <h:selectBooleanCheckbox value="#{_song.selectedForAlbum}"  disabled="#{not empty _song.parentAlbum}" />

      = Added a button in the SongList.xhtml for creating album:

      <s:button
               action="#{songList.createAlbum}"
               id="createAlbum"
               value="Create album">
              <f:param name="albumId" />
      </s:button>

      = Added the following field and method in the SongList.java

      @In(create=true)
      private AlbumHome albumHome;

      public String createAlbum(){
          for (Song s : getResultList()){
             if (s.getSelectedForAlbum()){
                 albumHome.getSongs().add(s);
             }
          }

          return "createdAlbum";
      }


      At the run time, there was always NPE caused by the line "for (Song s : getResultList())". The getResultList() returned null. I guess the EntityManager was somehow closed at this moment.

      There are other ways to implement this kind of use case (the Seam DVD example has a similar use case), but I find the automatically generated files look very fine, I'd like to learn how to build business logics on top of it. Why was there this NPE and how should I fix the NPE problem? Any important code to write in the page navigation descriptor?

      Many thanks in advance!


      - Ellen
        • 1. Re: resultList scope question
          ellenzhao
          Fixed it...well it was rather silly of me. First I forgot the surround the data table with the <h:form> tag, second I should not use <s:button> since it does not submit the form, use <h:commandButton> instead and now it works like charm, yea!