1 Reply Latest reply on Jan 11, 2007 7:16 AM by wolfc

    create() throws NullPointerException

    paras.java

      Hi,

      I am using Jboss 4.0.3, i have developed one simplee ejb application. it is successfully deployed. But when i run the application it throws null pointer exception. Please see the code, the exception is thrown at the line marked in red.

      /*
      * SessionTestServlet.java
      *
      */

      package test.session;

      import javax.servlet.*;
      import javax.servlet.http.*;
      import java.io.*;
      import javax.naming.*;
      import javax.rmi.PortableRemoteObject;

      public class SessionTestServlet extends HttpServlet {
      MyTestSessionHome testSessionBean;

      public void init(ServletConfig config) throws ServletException{
      //Look up home interface
      try {
      InitialContext ctx = new InitialContext();
      Object objref = ctx.lookup("ejb/test/MyTestSessionBean");
      testSessionBean = (MyTestSessionHome)PortableRemoteObject.narrow(objref, MyTestSessionHome.class);
      } catch (Exception NamingException) {
      NamingException.printStackTrace();
      }


      }

      public void service(HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException
      {

      PrintWriter out;
      response.setContentType("text/html");
      String title = "EJB Example";
      out = response.getWriter();

      out.println("");
      out.println("");
      out.println("Hello World Servlet!");
      out.println("");
      out.println("");
      out.println("<p align=\"center\"><font size=\"4\" color=\"#000080\">Servlet Calling Session Bean");


      try{
      MyTestSession beanRemote;
      System.out.println("Before create");
      beanRemote = testSessionBean.create();
      System.out.println("After create");
      out.println("<p align=\"center\"> Message from Session Bean is: " + beanRemote.SayHi() + "");
      beanRemote.remove();
      }catch(Exception CreateException){
      CreateException.printStackTrace();
      }
      out.println("<p align=\"center\"><a href=\"javascript:history.back()\">Go to Home");
      out.println("");
      out.println("");


      out.close();
      }

      public void destroy() {
      System.out.println("Destroy");
      }
      }


        • 1. Re: create() throws NullPointerException
          wolfc

          Most likely the lookup in init fails. Is there an exception before the NPE?

          You could change the code in init to:

          try
          {
           ...
          }
          catch(Exception e)
          {
           e.printStackTrace();
           throw new ServletException(e);
          }

          This should prevent a servlet to become alive after a fault.

          If a NamingException is the case, you should find the correct name using JNDIView in JMX-Console.