persistence@glassfish.java.net

NullPointerException

From: Roan Brasil Monteiro <roanbrasil_at_gmail.com>
Date: Thu, 22 Oct 2009 17:43:06 -0200

I have a class BaseDAO as bellow and I am getting NullPointerException on
EntityManager em = emf.createEntityManager(); line from BaseDAO class, Can
someone help me how to fix it?

public class BaseDAO<T> {

    @PersistenceUnit(unitName = "citespacePU")
    EntityManagerFactory emf;

    public List<T> ListCountry(){

        EntityManager em = emf.createEntityManager();

        try{
            Query q = em.createQuery("Select c from Country c ");
            List<Country> c = q.getResultList();
            return (List<T>) c;
        }finally{
                if (em != null && em.isOpen()) {
                    em.close();
                }
        }

    }


}

I have my Jersey resource class:

@Path("/user/")
public class UserResource {

    @GET
    @Produces("text/plain")
    public String registerUser(){


           BaseDAO<Country> countryDAO = new BaseDAO<Country>();
           List<Country> c = countryDAO.ListCountry();
            String y = "";

              for(Country x: c){
                  y = y + ":" +x.getName();
              }

        return "[Country List]:"+y;

    }

-- 
Atenciosamente,
Roan Brasil Monteiro
http://roanbrasil.wordpress.com/