5 Replies Latest reply on Jul 23, 2009 5:22 AM by Matt Higgins

    Null pointer when accessing entity manager in abstract class

    Matt Higgins Newbie
      I have the following situation. I have a DAO (service) that extends an abstract service that has the basic features for saving and updating entities in which I grab an entity manager and try to do work. I can use the entity manager fine in the concrete class but not in the abstract class where I get a null pointer. Here is the relevant code and stack trace. I hope this is not to much info.

      Using seam 2.1.2

      Here is the Service (DAO)
      @Name("venueService")
      public class VenueService extends AbstractEntityService<Venue> {
           
           @SuppressWarnings("unchecked")
           public List<Venue> findByCompanyId(Long companyId){
                return getEntityManager().createQuery("from Venue where company.id = :id").setParameter("id",companyId).getResultList();
           }

      }

      Note where I get the entity manager.. I have tried using @In with the same result.
      public abstract class AbstractEntityService<T> implements EntityService<T> {
           
           @Logger Log log;
           
           
           private Class<T> persistentClass;
           
           public T findById(Long id){
                return getEntityManager().find(persistentClass, id);
           }
         public EntityManager getEntityManager(){
               
               return (EntityManager) Component.getInstance("entityManager");
          }
         
      }

      @Path("/user/{username}/{password}")
      @Name("userResource")
      public class UserResource {

           @Logger
           Log log;
           
           @In(create=true)
           UserService userService;
           
           @In(create = true)
           VenueService venueService;

      @GET
           @Path("/favorite/{favoriteId}")
           @Produces("application/xml")
           public VenueDto getFavorite(@PathParam("username") String username, @PathParam("password") String password, @PathParam("favoriteId") Long favoriteId){
                User user = userService.findUserByUnameOrEmail(username, username);
                VenueDto venueDto = new VenueDto();
                if(userService.validatePassword(user, password)){
                     Venue venue = venueService.findById(favoriteId);
                     if(venue != null){
                          venueDto = venueDtoBuilder.buildDto(venue);
                     }
                }
                return venueDto;
           }
           
      }


      java.lang.NullPointerException
           at org.hibernate.impl.SessionImpl.get(SessionImpl.java:835)
           at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
           at org.jboss.seam.persistence.EntityManagerProxy.find(EntityManagerProxy.java:87)
           at com.swarm.service.AbstractEntityService.findById(AbstractEntityService.java:22)
        • 1. Re: Null pointer when accessing entity manager in abstract class
          Arbi Sookazian Master

          what happens if you remove the abstract keyword from your class?


          public class AbstractEntityService<T> implements EntityService<T> {...}

          • 2. Re: Null pointer when accessing entity manager in abstract class
            Arbi Sookazian Master

            Well I just tried this and it works fine:


            public abstract class TestAbstractEntityManagerFactory {
                 @Logger private Log log;
                 
                 public EntityManager getEntityManager(){
                     return (EntityManager) Component.getInstance("entityManager");
                 }
                 
            }



            @Name("myTest1")
            @Scope(ScopeType.CONVERSATION)
            public class TestApplicationMetaDataAction extends TestAbstractEntityManagerFactory {
            
                private EntityManager entityManager;
            
                @DataModel
                private List<ApplicationMetaData> apmdList_main;
            
                @Begin(join=true, flushMode=FlushModeType.MANUAL)
                @Factory("apmdList_main")
                public void getDataTable(){
                      
                      entityManager = super.getEntityManager();
                      
                      if (apmdList_addRow != null && apmdList_addRow.size() > 0)
                           apmdList_addRow.clear();
                      
                      apmdList_main = entityManager.createQuery("from ApplicationMetaData").getResultList();
                      
                }
            
            }



            I learned that you'll get a deployment exception if you add a @Name annotation to an abstract class...


            Now I'm wondering if an interface can be a Seam component...???


            survey says.......... no, that's illegal as well...


            Caused by: java.lang.IllegalArgumentException: component class is an interface: testInterface

            • 3. Re: Null pointer when accessing entity manager in abstract class
              Matt Higgins Newbie

              What you need to do is put a function in the abstract class make use of the entity manager and execute from the concrete class.


              I also tried the other idea of making the abstract class concrete and I have the same problem.


              • 4. Re: Null pointer when accessing entity manager in abstract class
                Matt Higgins Newbie

                I was a little hasty in that last response. To test you need to execute one of the abstract methods from another class. In the example above I execute venueService.findById(venueId) which is only on the abstract class.


                And thank for responding so fast! 

                • 5. Re: Null pointer when accessing entity manager in abstract class
                  Matt Higgins Newbie

                  I am still having an issue with this. I am hoping to get clarification on how / where the injection of the entity manager occurs. If I have an abstract class with an @In annotation it appears I can not use the injected object (in this case the enityManager) the abstract class, only in the concrete class.