persistence@glassfish.java.net

Re: NullPointerException

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

How should I resolve it? How should be my class? I am learning and I am
trying to put the persistence part in DAO class.

2009/10/22 Marina Vatkina <Marina.Vatkina_at_sun.com>

> 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/
>>
>
>


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