5 Replies Latest reply on Nov 5, 2008 8:12 PM by Andrew Rubinger

    Injecting EJB References NullPointerException JBoss 5.0 in S

    Matthew Purdy Newbie

      When using @EJB for a session bean in a servlet i get a NullPointerException - Am I missing something (new to Jee5 and Injections)

      HelloWorldSession is the interface for HelloWorldSessionBean

      
      public class HelloWorldCallingSessionEJBServlet extends HttpServlet
      {
       @EJB
       private HelloWorldSession helloWorldSession;
      
       public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
       {
       this.doWork(request, response);
      
       }//end method doGet
      
       public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
       {
       this.doWork(request, response);
      
       }//end method doPost
      
       private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
       {
      ;
       String helloString = "empty string";
      
       helloString = this.helloWorldSession.sayHello();
      
       response.setContentType("text/html");
       PrintWriter out = response.getWriter();
      
       out.println("<html><head><title>HelloWorld</title></head><body>");
       out.println("<h3>" + helloString + "</h3>");
       out.println("</body></html>");
      
      
       }//end method doWork
      
      
      }//end class HelloWorldCallingSessionEJBServlet
      


        • 1. Re: Injecting EJB References NullPointerException JBoss 5.0
          Peter Johnson Master

          You should provide a mappedName for @EJB, because the default name (HelloWorldSession) is typically not a valid JNDI name for looking up an EJB (most likely, it is something like xxx/HelloWorldSession/local, where xxx is the app name).

          You can use JNDIView to determine the deployed EJB's JNDI name. http://www.jboss.org/community/docs/DOC-9584

          • 2. Re: Injecting EJB References NullPointerException JBoss 5.0
            Matthew Purdy Newbie

             

            "PeterJ" wrote:
            You should provide a mappedName for @EJB, because the default name (HelloWorldSession) is typically not a valid JNDI name for looking up an EJB (most likely, it is something like xxx/HelloWorldSession/local, where xxx is the app name).

            You can use JNDIView to determine the deployed EJB's JNDI name. http://www.jboss.org/community/docs/DOC-9584


            thanx for your help - found the following:

            +- prj (class: org.jnp.interfaces.NamingContext)
            | +- HelloWorldSessionBean (class: org.jnp.interfaces.NamingContext)
            | | +- local (class: Proxy for: prj.ejb3.HelloWorldSession)
            | | +- local-prj.ejb3.HelloWorldSession (class: Proxy for: prj.ejb3.HelloWorldSession)


            here is the output at deployment:


            name=HelloWorldSessionBean,service=EJB3
            18:27:23,945 INFO [TomcatDeployment] deploy, ctxPath=/prj, vfsUrl=prj.ear/prjWEB.war
            18:27:54,210 INFO [TomcatDeployment] undeploy, ctxPath=/prj, vfsUrl=prj.ear/prjWEB.war
            18:27:54,220 INFO [SessionSpecContainer] Stopping jboss.j2ee:ear=prj.ear,jar=prjEJB.jar,name=HelloWorldSessionBean,service=EJB3
            18:27:54,221 INFO [EJBContainer] STOPPED EJB: prj.ejb3.HelloWorldSessionBean ejbName: HelloWorldSessionBean
            18:27:54,368 WARN [EjbMetadataJndiPolicyDecoratorDeployer] Defaulting to DefaultJndiBindingPolicy of "org.jboss.metadata.ejb.jboss.jndipolicy.plugins.BasicJndi
            BindingPolicy" for Session Bean HelloWorldSessionBean
            18:27:54,398 INFO [JBossASKernel] Created KernelDeployment for: prjEJB.jar
            18:27:54,398 INFO [JBossASKernel] installing bean: jboss.j2ee:ear=prj.ear,jar=prjEJB.jar,name=HelloWorldSessionBean,service=EJB3
            18:27:54,398 INFO [JBossASKernel] with dependencies:
            18:27:54,398 INFO [JBossASKernel] and demands:
            18:27:54,398 INFO [JBossASKernel] jboss.ejb:service=EJBTimerService
            18:27:54,398 INFO [JBossASKernel] and supplies:
            18:27:54,399 INFO [JBossASKernel] Class:prj.ejb3.HelloWorldSession
            18:27:54,399 INFO [JBossASKernel] jndi:prj/HelloWorldSessionBean/local
            18:27:54,399 INFO [JBossASKernel] jndi:prj/HelloWorldSessionBean/local-prj.ejb3.HelloWorldSession
            18:27:54,399 INFO [JBossASKernel] jndi:prj/HelloWorldSessionBean/remote
            18:27:54,399 INFO [JBossASKernel] Added bean(jboss.j2ee:ear=prj.ear,jar=prjEJB.jar,name=HelloWorldSessionBean,service=EJB3) to KernelDeployment of: prjEJB.jar
            18:27:54,453 INFO [SessionSpecContainer] Starting jboss.j2ee:ear=prj.ear,jar=prjEJB.jar,name=HelloWorldSessionBean,service=EJB3
            18:27:54,454 INFO [EJBContainer] STARTED EJB: prj.ejb3.HelloWorldSessionBean ejbName: HelloWorldSessionBean
            18:27:54,454 WARN [SessionSpecContainer] Populating JBoss-specific annotation metadata manually until done by deployers: jboss.j2ee:ear=prj.ear,jar=prjEJB.jar,
            name=HelloWorldSessionBean,service=EJB3
            18:27:54,489 INFO [TomcatDeployment] deploy, ctxPath=/prj, vfsUrl=prj.ear/prjWEB.war
            18:28:16,327 ERROR [[HelloWorldCallingSessionEJBServlet]] Servlet.service() for servlet HelloWorldCallingSessionEJBServlet threw exception
            java.lang.NullPointerException
            at prj.web.HelloWorldCallingSessionEJBServlet.doWork(HelloWorldCallingSessionEJBServlet.java:34)
            at prj.web.HelloWorldCallingSessionEJBServlet.doGet(HelloWorldCallingSessionEJBServlet.java:19)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
            at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:189)
            at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:91)
            at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:92)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
            at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:325)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
            at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
            at java.lang.Thread.run(Thread.java:619)


            from the above information i used:
             @EJB(mappedName="prj/HelloWorldSessionBean/local")
             private HelloWorldSession helloWorldSession;
            



            it still doesnt work:-( am i still missing something.

            • 3. Re: Injecting EJB References NullPointerException JBoss 5.0
              Peter Johnson Master

              See if this discussion (and the referenced discussions) helps: http://www.jboss.com/index.html?module=bb&op=viewtopic&t=143607

              (Yes, I am being lazy...)

              • 4. Re: Injecting EJB References NullPointerException JBoss 5.0
                Matthew Purdy Newbie

                 

                "PeterJ" wrote:
                See if this discussion (and the referenced discussions) helps: http://www.jboss.com/index.html?module=bb&op=viewtopic&t=143607

                (Yes, I am being lazy...)


                np: you learn more when you find it yourself!

                thanx so much; my problem was i needed to update the schema from 2.3 to 2.5...



                • 5. Re: Injecting EJB References NullPointerException JBoss 5.0
                  Andrew Rubinger Master

                   

                  "mpurdy1973" wrote:
                  thanx so much; my problem was i needed to update the schema from 2.3 to 2.5...


                  Also, the @EJB Resolver should be able to find the correct JNDI name for you, should not be required to specify "mappedName".

                  S,
                  ALR