persistence@glassfish.java.net

Re: NullPointerException

From: Michael Bar-Sinai <mich.barsinai_at_gmail.com>
Date: Thu, 22 Oct 2009 22:38:56 +0200

Are you sure the emf is null?
Emit a log message asserting that before you call return the DAO.
Take a look at the server logs, see if you get any injection error.
Make sure the unitName is correct.

You might want to restart glassfish, sometimes things get stuck in the
deployment system.

-- Michael

On 22 Oct, 2009, at 10:31 PM, Roan Brasil Monteiro wrote:

> I make as you told me and I keep to get NullPointerException, I know
> is injection problem because when I put this select query on
> resource class work right. Some idea?
>
> 2009/10/22 Roan Brasil Monteiro <roanbrasil_at_gmail.com>
> One Question:
>
> Should I create a interface
>
> @Local
> DaoResourceProviderLocal {
> public BaseDAO getBaseDAO();
> }
>
> @Stateless
> public class DaoResourceProvider implements DaoResourceProviderLocal{
>
> @PersisteceUnit(unitName="citespacePU")
> EntityManagerFactory emf;
>
> ...
> public BaseDAO getBaseDao() {
> BaseDAO retVal = new BaseDAO();
> retVal.setEmf( emf );
> return retVal;
> }
>
> }
>
>
> 2009/10/22 Michael Bar-Sinai <mich.barsinai_at_gmail.com>
> You can get the injected object using an EJB, and then manually
> inject them to the DAO:
> (This is from the top of my head, no JEE env here):
>
> @Local
> public class DaoResourceProvider implements DaoResourceProviderLocal {
>
> @PersisteceUnit(unitName="citespacePU")
> EntityManagerFactory emf;
>
> ...
> public BaseDAO getBaseDao() {
> BaseDAO retVal = new BaseDAO();
> retVal.setEmf( emf );
> return retVal;
> }
>
> ...
>
>
> --Michael
>
>
>
> On 22 Oct, 2009, at 10:07 PM, Roan Brasil Monteiro wrote:
>
>> Chapter 24?
>>
>> 2009/10/22 Roan Brasil Monteiro <roanbrasil_at_gmail.com>
>> Just a part. I have a JEE aplication and I would like to split the
>> persistence part from my JPA class. Do you have some idea? I will
>> take a look on the documentation.
>>
>>
>> 2009/10/22 Marina Vatkina <Marina.Vatkina_at_sun.com>
>> It depends on what you are trying to achieve in which type of an
>> application. Did you look at the JPA cjapter in Java EE 5 Tutorial?
>>
>> Roan Brasil Monteiro wrote:
>> 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 <mailto: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/
>>
>>
>>
>>
>> --
>> Atenciosamente,
>>
>> Roan Brasil Monteiro
>> http://roanbrasil.wordpress.com/
>>
>>
>>
>> --
>> Atenciosamente,
>>
>> Roan Brasil Monteiro
>> http://roanbrasil.wordpress.com/
>
>
>
>
> --
> Atenciosamente,
>
> Roan Brasil Monteiro
> http://roanbrasil.wordpress.com/
>
>
>
> --
> Atenciosamente,
>
> Roan Brasil Monteiro
> http://roanbrasil.wordpress.com/