5 Replies Latest reply on Feb 7, 2007 9:23 PM by dago

    NullPointerException when get a row from a list

    dago

      here is a class
      package book;

      import static javax.ejb.TransactionAttributeType.REQUIRES_NEW;
      import static org.jboss.seam.ScopeType.SESSION;

      import java.io.Serializable;
      import java.util.List;

      import javax.ejb.Remove;
      import javax.ejb.Stateful;
      import javax.ejb.TransactionAttribute;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;

      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Destroy;
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Observer;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.datamodel.DataModel;
      import org.jboss.seam.annotations.datamodel.DataModelSelection;
      import org.jboss.seam.core.FacesMessages;
      import org.jboss.seam.log.Log;

      @Stateful
      @Scope(ScopeType.SESSION)
      @LoggedIn
      @Name("cnMessageActionTest")
      public class CnMessageActionTest implements CnMessageTestInterface
      {
      @PersistenceContext
      private EntityManager em;

      @DataModel
      private List cnMessages;

      //@Out(required=false)
      //@PersistenceContext
      private CnMessage cnMessage;

      @Factory
      //@Observer("CnMessage hello")
      public void getCnMessages()
      {
      cnMessages = em.createQuery("from CnMessage").getResultList();
      //cnMessage = cnMessages.get(0);
      }
      public void getCnMessage(String enMsg){
      cnMessage = cnMessages.get(1);

      //return "#{cnMessages.cnMsg}";
      }


      @Destroy @Remove
      public void destroy()
      {}
      }

      when i call the method "getCnMessage" from another class


      public class UITicker extends UIOutput {
      private CnMessageActionTest cmat;


      public void encodeBegin(FacesContext ctx) throws IOException {

      ResponseWriter writer = ctx.getResponseWriter();
      writer.startElement("div", this);

      String enMsg = (String)getAttributes().get("enMsg");

      cmat = new CnMessageActionTest();
      cmat.getCnMessage(enMsg);


      if (enMsg!=null)
      writer.writeAttribute("enMsg", enMsg, null);


      String style = (String)getAttributes().get("style");
      if (style!=null)
      writer.writeAttribute("style", style, null);

      String styleClass = (String)getAttributes().get("styleClass");
      if (styleClass!=null)
      writer.writeAttribute("class", styleClass, null);
      }

      public void encodeEnd(FacesContext context) throws IOException {
      ResponseWriter writer = context.getResponseWriter();
      writer.endElement("div");
      }
      }

      it thow exception:

      java.lang.NullPointerException
      at book.CnMessageActionTest.getCnMessage(CnMessageActionTest.java:60)
      at ticker.UITicker.encodeBegin(UITicker.java:55)
      at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:232)
      at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:239)
      at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:554)
      at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:352)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:107)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at book.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:42)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.jboss.seam.servlet.SeamRedirectFilter.doFilter(SeamRedirectFilter.java:30)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
      at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
      at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
      at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
      at java.lang.Thread.run(Thread.java:619)

        • 1. Re: NullPointerException when get a row from a list
          smokingapipe

          If the cnMessages component has not been accessed from a page, then Seam will not have initialized it (ie, called the factory). Just because your other class is trying to access it, Seam doesn't know anything about that and won't magically call the factory method. That class will have to call the factory method manually, or do it some other way.

          • 2. Re: NullPointerException when get a row from a list
            dago

            thank u SmokingAPipe!

            you say:
            "That class will have to call the factory method manually, or do it some other way"

            how can i do it?

            • 3. Re: NullPointerException when get a row from a list
              smokingapipe

              You need to have a @Begin method that initializes the cnMessages member. You're calling getCnMessage() before cnMessages has been set to anything. Tell me where is cnMessages being set? If you can answer that question you'll see what's going on.

              • 4. Re: NullPointerException when get a row from a list
                dago

                hello SmokingAPipe.

                you said:

                Tell me where is cnMessages being set?


                see the red code bellow:

                public class UITicker extends UIOutput {
                private CnMessageActionTest cmat;

                public void encodeBegin(FacesContext ctx) throws IOException {

                ResponseWriter writer = ctx.getResponseWriter();
                writer.startElement("div", this);

                String enMsg = (String)getAttributes().get("enMsg");

                cmat = new CnMessageActionTest();
                cmat.getCnMessage(enMsg);

                if (enMsg!=null)
                writer.writeAttribute("enMsg", enMsg, null);

                String style = (String)getAttributes().get("style");
                if (style!=null)
                writer.writeAttribute("style", style, null);

                String styleClass = (String)getAttributes().get("styleClass");
                if (styleClass!=null)
                writer.writeAttribute("class", styleClass, null);
                }

                public void encodeEnd(FacesContext context) throws IOException {
                ResponseWriter writer = context.getResponseWriter();
                writer.endElement("div");
                }
                }

                thanks! (I am a newer to seam)

                • 5. Re: NullPointerException when get a row from a list
                  dago

                  this is the javabean:

                  @Entity
                  @Name("cnMessage")
                  public class CnMessage implements Serializable
                  {
                   private int idt;
                   private String cnMsg;
                   private String enMsg;
                  
                  
                   public CnMessage() {}
                  
                   public CnMessage(
                   int idt,
                   String cnMsg,
                   String enMsg)
                   {
                   this.idt = idt;
                   this.cnMsg = cnMsg;
                   this.enMsg = enMsg;
                   }
                  
                   @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
                   public int getIdt()
                   {
                   return idt;
                   }
                  
                   public void setIdt(int idt)
                   {
                   this.idt=idt;
                   }
                  
                   public String getCnMsg()
                   {
                   return cnMsg;
                   }
                  
                   public void setCnMsg(String cnMsg)
                   {
                   this.cnMsg=cnMsg;
                   }
                  
                   public String getEnMsg()
                   {
                   return enMsg;
                   }
                  
                   public void setEnMsg(String enMsg)
                   {
                   this.enMsg=enMsg;
                   }
                  
                  
                  
                  
                  }