4 Replies Latest reply on Apr 5, 2011 11:19 PM by Rino Finazzo

    EntityManager not injected and nullpointer

    Rino Finazzo Newbie

      Hello

       

      I have a simple Ejb application consisting of 3 entities and 3 stateless SB, web tier is based on a jsp page which accesses the  DB making some simple operations.

      The application has been deployed as an ear  file to jboss-5.1.0.GA.

      The only jar on default/lib directory is the mysql connector mysql-connector-java-5.1.15-bin.jar, used to map queries to a mysql dbms.

       

      Here is my persistence.xml

       

      <?xml version="1.0" encoding="UTF-8"?>
      
       <persistence xmlns="http://java.sun.com/xml/ns/persistence"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
              version="1.0">
          <persistence-unit name="SampleProjectUnit" >
              <provider>org.hibernate.ejb.HibernatePersistence</provider>
              <jta-data-source>java:/MyDB</jta-data-source>        
              <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
              </properties>
          </persistence-unit>
      </persistence>
      

       

      The Error i get from logs is java.lang.NullPointerException while invoking em.createQuery, where em is my container-managed entity manager, using jdpa i can see em as a null pointer, as not being injected.

       

      This is the SB where i call the entitymanager :

       

       

      @Stateless @TransactionManagement(TransactionManagementType.CONTAINER)
      
      public class PublisherDAOBean implements PublisherDAO {
      
          @PersistenceContext(unitName = "SampleProjectUnit")EntityManager em;
      
      
          @SuppressWarnings("unchecked")
          public List<Publisher> findAllPublishers() {
              return em.createQuery("SELECT a FROM Publisher a" ).getResultList(); // here i get the nullpointer exception
          }
      
      ...
      
      
      

       

      Can anyone tell me how to solve this issue ?

       

      Thanks, Rino

       

      Attached files:

       

      Progetto_EJB3.ear is the app as deployed to jboss

      source.zip contains the source files

      server.log is the jboss log from server startup

        • 1. Re: EntityManager not injected and nullpointer
          jaikiran pai Master

          Rino, welcome to the forums!

           

           

          public class EJB3DaoFactory extends DAOFactory {
          
              @Override
              public AuthorDAO getAuthorDAO() {
                  // TODO Auto-generated method stub
                  return new AuthorDAOBean();
              }
          
              @Override
              public BookDAO getBookDAO() {
                  // TODO Auto-generated method stub
                  return new BookDAOBean();
              }
          
              @Override
              public PublisherDAO getPublisherDAO() {
                  // TODO Auto-generated method stub
                  return new PublisherDAOBean();
              }
          
          }
          

           

          Never instantiate (using "new") a container managed component (like an EJB). The container (application server) is responsible for doing the instantiation and injecting it with the necessary resources and give you that fully injected instance. Typically, such a injected instance of the managed component is available through JNDI. So all you have to do is lookup that instance in JNDI at  a some specific JNDI key.

           

          See these for more details:

           

          http://community.jboss.org/message/367375#367375

          http://community.jboss.org/thread/110303?tstart=0#4158957

          • 2. Re: EntityManager not injected and nullpointer
            Rino Finazzo Newbie

            Thanks for the reply,

             

            I had forgotten to make the container inject those SB instances in the factory before calling those getter methods.

            Now instead of calling a classical JNDI lookup, i would rather use a @EJB annotation to have those SB injected, i've tried this approach but unfortunately i get a nullpointer too:

             

             

             

            public class EJB3DaoFactory extends DAOFactory {
            @EJB private AuthorDAOBean a; 
            @EJB private BookDAOBean b;
            @EJB private PublisherDAOBean p;
            
            //a,b and p turn out to be null
            
                @Override
                public AuthorDAO getAuthorDAO() {
                    return a;
                }
            
                @Override
                public BookDAO getBookDAO() {
                    return b;
                }
            
                @Override
                public PublisherDAO getPublisherDAO() {
                    return p;
                }
            }
            

             

            How can i fix it ?

            • 3. Re: EntityManager not injected and nullpointer
              jaikiran pai Master

              That's because injection works only in container managed components. EJB3DaoFactory isn't a container managed component. If you want to access those beans in a non-container managed class like that factory, then use the JNDI lookup.

              • 4. Re: EntityManager not injected and nullpointer
                Rino Finazzo Newbie

                Ok, everything is working now,

                 

                in order to use @EJB  i had to rearrange the whole project having the SB directly injected in a servlet like this:

                 

                 

                public class EJBServlet extends HttpServlet {
                
                
                    private static final long serialVersionUID = -3583166529015480243L;
                    @EJB(mappedName= "Progetto_EJB3/BookDAOBean/local") 
                    private BookDAO bookSB;
                    @EJB(mappedName= "Progetto_EJB3/PublisherDAOBean/local")
                    private PublisherDAO publisherSB;
                    @EJB(mappedName= "Progetto_EJB3/AuthorDAOBean/local")
                    private AuthorDAO authorSB;
                

                 

                and then forward the fetched data to a jsp page according to MVC pattern.

                 

                Thanks