persistence@glassfish.java.net

Re: NullPointerException

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Thu, 22 Oct 2009 12:52:56 -0700

You can't inject anything in a class that is neither a Main class, nor a JavaEE
component.

-marina

Roan Brasil Monteiro wrote:
> 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/