2 Replies Latest reply on Jun 15, 2012 4:40 AM by morphy

    java.lang.NullPointerException in EJB3 from JSP page

    mmunir

      I am developing my first EJB 3 application using JBOSS 7 as application server in Eclipse. The session bean is deployed successfully on the server but when i try to access it from the JSP page, then i get the java.lang.NullPointerException exception.

      Following is my code:

      Local Interface:

      package my.first;

      import javax.ejb.Local;

      @Local
      public interface CalculatorRemote {

           public float add(float x, float y);

           public float subtract(float x, float y);

           public float multiply(float x, float y);

           public float division(float x, float y);
      }

      Session bean:

       

      package my.first;

          import javax.ejb.LocalBean;
          import javax.ejb.Stateless;

          @Stateless(name = "CalculatorRemote")
          public class CalculatorBean implements CalculatorRemote {
              public float add(float x, float y) {
                  return x + y;
              }

              public float subtract(float x, float y) {
                  return x - y;
              }

              public float multiply(float x, float y) {
                  return x * y;
              }

              public float division(float x, float y) {
                  return x / y;
              }
          }

      JSP Page:

           <%!
            private CalculatorRemote calculator = null;
            float result=0;

            public void jspInit() {
            try {

            InitialContext ic = new InitialContext();

            calculator = (CalculatorRemote) ic
            .lookup("CalculatorRemote/Local");


            System.out.println("Loaded Calculator Bean");
          //CalculatorBean

            } catch (Exception ex) {
            System.out.println("Error:"+
            ex.getMessage());
            }
            }
            public void jspDestroy() {
            calculator = null;
            }
          %>

      Server Log File:

      17:17:20,552 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-4) JNDI bindings for session bean named CalculatorRemote in deployment unit deployment "EJBsession.jar" are as follows:

              java:global/EJBsession/CalculatorRemote!my.first.CalculatorBean
              java:app/EJBsession/CalculatorRemote!my.first.CalculatorBean
              java:module/CalculatorRemote!my.first.CalculatorBean
              java:global/EJBsession/CalculatorRemote!my.first.CalculatorRemote
              java:app/EJBsession/CalculatorRemote!my.first.CalculatorRemote
              java:module/CalculatorRemote!my.first.CalculatorRemote
              ......
              ......
              ......

              17:23:44,105 ERROR [stderr] (http--127.0.0.1-8080-1) java.lang.NullPointerException

              17:23:44,105 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.apache.jsp.test_jsp._jspService(test_jsp.java:102)

              17:23:44,105 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

              17:23:44,105 ERROR [stderr] (http--127.0.0.1-8080-1)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

              ......
              ......
              ......

       

      Please help?

        • 1. Re: java.lang.NullPointerException in EJB3 from JSP page
          amarnath.k86

          steps

           

          1. create a servlet class, move your jsp scriptlet code to servlet.

          2. configure servlet class into web.xml file.

          3. create a form have value1, value2 textbox and submit button.

          4. press the submit button call the servlet url which you configured in web.xml.

          5. once process done in servlet class, pass response as html.

          6. Get the response from jsp page, and print.

          1 of 1 people found this helpful
          • 2. Re: java.lang.NullPointerException in EJB3 from JSP page
            morphy

            ignoring your jsp (ancient) approach, try to lookup the following jndi name: java:global/EJBsession/CalculatorRemote!my.first.CalculatorBean

             

            next step, improve your development paradigm

             

            bye

            1 of 1 people found this helpful