5 Replies Latest reply on Jul 23, 2009 5:22 AM by mhiggins.matt.smartdestinations.com

    Null pointer when accessing entity manager in abstract class

    mhiggins.matt.smartdestinations.com
      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)