7 Replies Latest reply on May 27, 2008 4:17 AM by julypyrra

    EntityManager throws a nullpointerexception JBOSS 4.2.2GA EJ

    julypyrra

      Hola,

      I'm developing web services using EJB3 and JBOSS 4.2.2.GA.

      I have a NPE which seems to come from a non working EntityManager injection. I'm not very comfortable with EJB3 yet...

      My web service works fine. In its method, it calls another class method which performs one part of the service. To interact with the database, it uses the methods of a class which regroups all the queries. Therefore, this is where i use an EntityManager as following:


      package bd.query;
      
      import java.util.List;
      
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      @Stateless
      public class Query implements QueryInt{
      
       @PersistenceContext
       EntityManager em;
      
       public SystemUser AuthenticateUser(String uid, String pwd){
       SystemUser _SystemUser = (SystemUser) em.createQuery("select a.systemUser from Account ").getSingleResult();
       return _SystemUser;
      
       }
      }
      


      This is my persistence.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <persistence>
       <persistence-unit name="Query" >
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/cuoreds</jta-data-source>
       </persistence-unit>
      </persistence>
      


      I package all my classes in a .jar file with the persistence.xml in a META-INF directory. It seems to deploy correctly but when I call my methods using my authentification query, I get the following error :
      2008-05-26 15:47:06,184 ERROR [org.jboss.ws.core.jaxws.SOAPFaultHelperJAXWS] SOAP request exception
      javax.ejb.EJBException: java.lang.NullPointerException
       at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:63)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
       at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.wsf.container.jboss42.InvocationHandlerEJB3.invoke(InvocationHandlerEJB3.java:103)
       at org.jboss.ws.core.server.ServiceEndpointInvoker.invoke(ServiceEndpointInvoker.java:220)
       at org.jboss.wsf.stack.jbws.RequestHandlerImpl.processRequest(RequestHandlerImpl.java:408)
       at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(RequestHandlerImpl.java:272)
       at org.jboss.wsf.stack.jbws.RequestHandlerImpl.doPost(RequestHandlerImpl.java:189)
       at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:122)
       at org.jboss.wsf.stack.jbws.EndpointServlet.service(EndpointServlet.java:84)
       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:230)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
       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:262)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
       at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
       at java.lang.Thread.run(Thread.java:595)
      Caused by: java.lang.NullPointerException
       at org.eu.nuadu.cuore.bd.query.Query.AuthenticateUser(Query.java:89)
       at org.eu.nuadu.cuore.ws.process.Authentication.authenticate(Authentication.java:24)
       at org.eu.nuadu.cuore.ws.services.Update.SearchEvent(Update.java:22)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
       at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
       at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
       ... 39 more
      


      I think the error comes from an architecture which doesn't fit EJB3 because it works with a EntityManagerFactory...

      I've looked for a solution on many forums but so far, it never appeared solve my problem. Please let me know if you have any idea...

        • 1. Re: EntityManager throws a nullpointerexception JBOSS 4.2.2G
          jaikiran

          The code looks fine to me (the injection part). Are there are exceptions when you deploy this application? And what does the following System.out print:

          public SystemUser AuthenticateUser(String uid, String pwd) {
           System.out.println("Entity manager injected is: " + em);
           SystemUser _SystemUser = (SystemUser) em.createQuery("select a.systemUser from Account ").getSingleResult();
           return _SystemUser;
          
           }


          The method AuthenticateUser is available through the QueryInt, isn't it? Can you post that interface? Also post the code where this bean is being looked up and used.

          • 2. Re: EntityManager throws a nullpointerexception JBOSS 4.2.2G
            julypyrra

            Thanks for your answer, that was fast!

            As previsted, the print is : Entity manager injected is: null
            At least it's coherent...

            As it seems that no entitymanager is created, could it come from the fact that it's injected in a class called by the main class (web service) and therefore not deployed or something like that?

            • 3. Re: EntityManager throws a nullpointerexception JBOSS 4.2.2G
              jaikiran

               

              "July Pyrra" wrote:
              could it come from the fact that it's injected in a class called by the main class (web service) and therefore not deployed or something like that?


              As long as you are "looking-up" an EJB (in this case the Query EJB), and not creating an instance yourself, the injection should work. That's the reason i was interested in the code which uses this EJB.

              • 4. Re: EntityManager throws a nullpointerexception JBOSS 4.2.2G
                julypyrra

                Ok sorry, I jumped the last part of your answer... That could be it because that thing of lookup is not very clear to me.

                My interface :

                package org.eu.nuadu.cuore.bd.query;
                import datamodel.DAO.SystemUser;
                
                public interface QueryInt {
                public abstract SystemUser AuthenticateUser(String uid, String pwd);
                }
                


                And code which uses the EJB:
                package ws.process;
                
                import org.eu.nuadu.cuore.bd.query.Query;
                import org.eu.nuadu.cuore.datamodel.DAO.SystemUser;
                
                public class Authentication {
                
                 public void authenticate(){
                
                 SystemUser _SystemUser = new Query().AuthenticateUser("july","1234");
                 if (_SystemUser != null)
                 System.out.println(_SystemUser.getIdSystemUser());
                }
                }
                


                • 5. Re: EntityManager throws a nullpointerexception JBOSS 4.2.2G
                  julypyrra

                  arrg, i forgot to update the name fo the import and package... it must happen... I believe you can guess...

                  • 6. Re: EntityManager throws a nullpointerexception JBOSS 4.2.2G
                    jaikiran

                     

                    SystemUser _SystemUser = new Query().AuthenticateUser("july","1234");


                    Here's the problem :)

                    You are using the Query class as just another java class and instantiating it on your own. As a result you are not getting access to the additional services which the application server injects in the EJB.

                    Any EJB clients, need to lookup the EJB to be able to avail the services provided by the server. You might have to read through some examples which explain how to lookup the EJB from a client. Usually, this is how it is done:

                    //assuming the server and the client are on the same server,
                    //else pass context properties
                    javax.naming.Context ctx = new javax.naming.InitialContext();
                    
                    QueryInt bean = (QueryInt) ctx.lookup("QueryBeanJNDIName");// this is the jndi-name to which the bean is bound.
                    
                    SystemUser _SystemUser = bean.AuthenticateUser("july","1234");
                    if (_SystemUser != null)
                     System.out.println(_SystemUser.getIdSystemUser());
                    


                    • 7. Re: EntityManager throws a nullpointerexception JBOSS 4.2.2G
                      julypyrra

                      Thank you! It seems to work now! At least I don't get the NullPointerException anymore so the injection worked.

                      To close the subject, here is my code with the lookup

                      package ws.process;
                      
                      import bd.query.Query;
                      import datamodel.DAO.SystemUser;
                      
                      public class Authentication {
                      
                       public void authenticate(){
                       Context context = new InitialContext();
                       QueryInt bean = (QueryInt) context.lookup("Query/local");
                       SystemUser _SystemUser = bean.AuthenticateUser("julie","1234");
                      
                       System.out.println(SystemUser.getId());
                       }
                      }
                      


                      package bd.query;
                      
                      import java.util.List;
                      import datamodel.DAO.SystemUser;
                      
                      import javax.ejb.Stateless;
                      import javax.persistence.EntityManager;
                      import javax.persistence.PersistenceContext;
                      
                      @Stateless
                      public class Query implements QueryInt{
                      
                       @PersistenceContext
                       EntityManager em;
                      
                       public SystemUser AuthenticateUser(String uid, String pwd){
                       SystemUser _SystemUser = (SystemUser) em.createQuery("select a.systemUser from Account ").getSingleResult();
                       return _SystemUser;
                      
                       }
                      }
                      


                      package bd.query;
                      
                      import datamodel.DAO.SystemUser;
                      import javax.ejb.Local;
                      
                      @Local
                      public interface QueryInt {
                      public abstract SystemUser AuthenticateUser(String uid, String pwd);
                      }
                      


                      and a very simple persistence.xml
                      <?xml version="1.0" encoding="UTF-8"?>
                      <persistence>
                      
                       <persistence-unit name="Nuadu">
                       <provider>org.hibernate.ejb.HibernatePersistence</provider>
                       <jta-data-source>java:/cuoreds</jta-data-source>
                       </persistence-unit>
                      
                      </persistence>
                      


                      Thanks again!