persistence@glassfish.java.net

Re: NullPointerException

From: Roan Brasil Monteiro <roanbrasil_at_gmail.com>
Date: Fri, 23 Oct 2009 12:52:00 -0200

Follow up the code. P.S. -> If I use the @PersistenceUnit or
@PersistenceContext on my Resource Class I got the result,
NullPointerException is occurring in this situation I am trying to use EJB.
Do you need persistence.xml or web.xml?

Interface BaseDAO

*package com.citespace.citespacerest.dao;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.persistence.EntityExistsException;
import javax.persistence.TransactionRequiredException;

@Remote
public interface BaseDAO<T> {
    public List<T> listCountry();
}*

-------------------------------8<-----------BaseDAOImpl
Class--------------8<-------------------------------8<-----------------------------------------------------

*package com.citespace.citespacerest.dao.impl;


import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;

import com.citespace.citespacerest.dao.BaseDAO;
import com.citespace.cms.model.Country;

@Stateless
public class BaseDAOImpl<T> implements BaseDAO<T>{


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

    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    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();
                }
        }

    }
}*

-------------------------------8<----------------Resource
Class------------------8<-------------------------------8<-----------------------------------------------------

*package com.citespace.citespacerest;

import java.util.List;

import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;


import com.citespace.citespacerest.dao.impl.BaseDAOImpl;
import com.citespace.cms.model.Country;

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

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

           BaseDAOImpl<Country> countryDAO = new BaseDAOImpl<Country>();
           List<Country> c = countryDAO.listCountry();
            String y = "";
              for(Country x: c){
                  y = y + ":" +x.getName();
              }

        return "[Lista de Países]:"+y;
    }

}*



2009/10/23 Eve Pokua <gorgeous65_at_msn.com>

> Roan
>
> I am trying to catch up.
>
> Please, send us
>
> a the code of your remote, interface
>
> then imple stateless bean class
>
> Then the servlet.
>
> Then I will following it up for you.
>
> Are you accessing your data from DB?
>
> Is it a war or ear at the moment. perhaps somebody maybe able to test it
> for you if you send some test codes
>
> eve
>
> ------------------------------
> Date: Fri, 23 Oct 2009 12:13:20 -0200
> From: roanbrasil_at_gmail.com
> To: persistence_at_glassfish.dev.java.net
> Subject: Re: NullPointerException
>
>
> I have one question, in case I have my web.xml using glassfish + jpa +
> jersey currently, and I added a ejb.jar i my project, Should I set up
> something no web.xml as on this injection? I am using Stateless in a class
> and I have a interface Remote and another one Local. But I keep to getting
> NullPointerException, I guess is something on web.xml. Can someone give me
> some help?
>
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
> http://java.sun.com/xml/ns/javaee
> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
> <servlet>
> <servlet-name>Jersey Web Application</servlet-name>
>
> <servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
> <init-param>
> <param-name>com.sun.jersey.config.feature.Redirect</param-name>
> <param-value>true</param-value>
> </init-param>
> <init-param>
> <param-name>unit:citespacePU</param-name>
> <param-value>persistence/citespace</param-value>
> </init-param>
> <load-on-startup>1</load-on-startup>
> </servlet>
> <servlet-mapping>
> <servlet-name>Jersey Web Application</servlet-name>
> <url-pattern>/resources/*</url-pattern>
> </servlet-mapping>
> <session-config>
> <session-timeout>
> 30
> </session-timeout>
> </session-config>
> <welcome-file-list>
> <welcome-file>
> index.jsp
> </welcome-file>
> </welcome-file-list>
> <persistence-unit-ref>
>
> <persistence-unit-ref-name>persistence/citespace</persistence-unit-ref-name>
> <persistence-unit-name>citespacePU</persistence-unit-name>
> </persistence-unit-ref>
> <resource-ref>
> <res-ref-name>UserTransaction</res-ref-name>
> <res-type>javax.transaction.UserTransaction</res-type>
> <res-auth>Container</res-auth>
> </resource-ref>
> </web-app>
>
>
>
>
>
> --
> Atenciosamente,
>
> Roan Brasil Monteiro
> http://roanbrasil.wordpress.com/
>
> ------------------------------
> New Windows 7: Simplify what you do everyday. Find the right PC for you.<http://www.microsoft.com/windows/buy/>
>



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