3 Replies Latest reply on Oct 9, 2005 9:38 PM by eokyere

    seam newbie| trivial example | NullPointerException

    eokyere

      I am attempting to create (what should be) a trivial Seam app, following/using some of the code from the hotel booking app, and I am consistently getting a NullPointerException for the first page it should display.

      my app has three classes (Calculator.java, Calculate.java and CalculateAction.java) as follows:

      Calculator.java:

      // Calculator.java
      
      package lab.seam.calc;
      
      import static org.jboss.seam.ScopeType.SESSION;
      
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.annotations.Scope;
      
      @Scope(SESSION)
      @Name("calc")
      public class Calculator {
       public Calculator() {
       }
      
       public void multiply() {
       result = firstNumber * secondNumber;
       }
      
       public void add() {
       result = firstNumber + secondNumber;
       }
      
       public void setFirstNumber(int n) {
       this.firstNumber = n;
       }
      
       public void setSecondNumber(int n) {
       this.secondNumber = n;
       }
      
       public int getFirstNumber() {
       return firstNumber;
       }
      
       public int getSecondNumber() {
       return secondNumber;
       }
      
       public int getResult() {
       return result;
       }
      
       @In
       @Out
       private int firstNumber;
       @In
       @Out
       private int secondNumber;
       @Out
       private int result;
      }
      
      



      Calculate.java:


      // Calculate.java
      
      package lab.seam.calc;
      
      import javax.ejb.Local;
      
      @Local
      public interface Calculate {
       public String add();
       public String multiply();
      }
      



      CalculateAction.java:
      //CalculateAction.java
      
      package lab.seam.calc;
      
      import static org.jboss.seam.ScopeType.EVENT;
      
      import javax.ejb.Interceptor;
      import javax.ejb.Stateless;
      
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.ejb.SeamInterceptor;
      
      @Stateless
      @Scope(EVENT)
      @Name("calculate")
      @Interceptor(SeamInterceptor.class)
      public class CalculateAction implements Calculate {
      
       public void setCalculator(Calculator c) {
       calc = c;
       }
      
       public String add() {
       calc.add();
       return SUCCESS;
       }
      
       public String multiply() {
       calc.multiply();
       return SUCCESS;
       }
      
       private static final String SUCCESS = "success";
      
       @In
       private Calculator calc;
      
      }
      




      I have three xhtml files: index.xhtml, calculator.xhtml, and results.xhtml

      calculator.xhtml looks like (as you can see, not much difference from the hotel app) :

      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition 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"
       template="template.xhtml">
      
      <!-- content -->
      <ui:define name="content">
      <div class="section">
       <h1>Calculator</h1>
      </div>
      <div class="section">
       <h:form>
       <fieldset>
       <div class="entry">
       <div class="label"><h:outputLabel for="firstNumber">1st Number:</h:outputLabel></div>
       <div class="input"><h:inputText id="firstNumber" value="#{calc.firstNumber}" /><br/>
       <span class="errors"><h:message for="firstNumber" /></span></div>
       </div>
       <div class="entry">
       <div class="label"><h:outputLabel for="secondNumber">2nd Number:</h:outputLabel></div>
       <div class="input"><h:inputText id="secondNumber" value="#{calc.secondNumber}" /><br/>
       <span class="errors"><h:message for="secondNumber" /></span></div>
       </div>
       <div class="entry errors"><h:messages globalOnly="true"/></div>
       <div class="entry">
       <div class="label"> </div>
       <div class="input">
       <h:commandButton value="Add" action="#{calculate.add}" class="button"/>
       <h:commandButton value="Multiply" action="#{calculate.multiply}" class="button"/>
       </div>
       </div>
       </fieldset>
       </h:form>
      </div>
      </ui:define>
      
      <!-- sidebar -->
      <ui:define name="sidebar">
      <h1>Info</h1>
      <p>
       info info info
      </p>
      </ui:define>
      
      </ui:composition>
      




      Now the problem:

      I can't seem to get calculator.jsf to display. I get a NullPointerException with the following stack trace

      java.lang.NullPointerException
       at org.apache.myfaces.application.jsp.JspStateManagerImpl.restoreTreeStructure(JspStateManagerImpl.java:141)
       at org.apache.myfaces.application.jsp.JspStateManagerImpl.restoreView(JspStateManagerImpl.java:181)
       at org.apache.myfaces.application.jsp.JspViewHandlerImpl.restoreView(JspViewHandlerImpl.java:246)
       at org.apache.myfaces.lifecycle.LifecycleImpl.restoreView(LifecycleImpl.java:113)
       at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:66)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:94)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
       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.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:157)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
       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:856)
       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
       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:595)
      




      I will really appreciate any pointers.

      Thanks,
      Emmanuel

        • 1. Re: seam newbie| trivial example | NullPointerException
          eokyere

          so 17 views, no replies... surely, this shd be as trivial as i am thinking?

          • 2. Re: seam newbie| trivial example | NullPointerException

            Since you are using Facelets, try specifying the facelets development mode to trace back to where the NullPointerException is... you can find the documentation here: facelets.dev.java.net

            Also, make sure you have the correct phase listeners setup as per the Seam documentation

            • 3. Re: seam newbie| trivial example | NullPointerException
              eokyere

              Thanks for the pointers hookomjj

              1. turns out jsf-facelets.jar wasn't being archived as part of my war file, and so the FaceletViewHandler could not be referenced; fix in the build file rectified that :)

              2. if any other newbies are trying out this example, make sure to remove the In/Out annotations in the Calculator class; ie.

              This:

               @In
               @Out
               private int firstNumber;
               @In
               @Out
               private int secondNumber;
               @Out
               private int result;
              


              should read:

               private int firstNumber;
               private int secondNumber;
               private int result;
              


              Thanks again hookomjj

              eokyere