persistence@glassfish.java.net

Re: NullPointerException

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

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>


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

>
> Take a look at this -
>
>
> http://www.jboss.org/file-access/default/members/jbossejb3/freezone/docs/tutorial/1.0.7/html/Injecting_EJB_in_Servlets.html
>
>
> ------------------------------
> From: gorgeous65_at_msn.com
> To: persistence_at_glassfish.dev.java.net
> Date: Fri, 23 Oct 2009 11:30:17 +0100
> Subject: RE: NullPointerException
>
>
> Hi,
>
> I am able to use injections with remote interface and it retrieves data for
> me.
>
> I am using a remote interface in my application.
>
> In your stateless bean implementation class, try and use @Persistence.
> Don't bother to supply it with the persistent unit name unless you
> have more than one persistence unit in your application.
>
>
> I assumed you are using servlet as the client.
>
> My application is also an ear.
>
> eve
>
>
> ------------------------------
> Date: Thu, 22 Oct 2009 19:47:02 -0200
> From: roanbrasil_at_gmail.com
> To: persistence_at_glassfish.dev.java.net
> Subject: Re: NullPointerException
>
> I am using glassfish v2.1, I will put here I did a @Stateless class.
>
> I make it
>
> @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 Marina Vatkina <Marina.Vatkina_at_sun.com>
>
> It depends on the GF version. In v3 which is being built to be Java EE 6
> compliant, EJBs can be part of a WAR file.
>
> Regards,
> -marina
>
> Michael Bar-Sinai wrote:
>
> Ah! web app.
> I don't think you get any injections in a web app, you need to have an EJB
> in an EJB module and then look it up (JNDI) from the web app.
> Is there a section called "Persistence in the Web Tier" or something like
> that??
> Do look in the log files, I think Glassfish would at least complain about
> having @Stateless classes in a deployed war.
>
> You can also try to create an .ear project instead of a .war one.
>
> -- Michael
>
> On 22 Oct, 2009, at 10:36 PM, Roan Brasil Monteiro wrote:
>
>
> it's a web application I am trying to write, I created a bean and deploy
> and this didn't work yet.
>
>
> 2009/10/22 Michael Bar-Sinai <mich.barsinai_at_gmail.com <mailto:
> mich.barsinai_at_gmail.com>>
>
> á áYes, the idea is to create an EJB - as Marina said, you need a JEE
> á áclass to get injections.
> á áSo that's the whole process here: create the interface and the
> á ábean, deploy, call the bean.
> á áPlease note that if you're just learning JPA and try to implement
> á áit in a desktop application, this is not the way at all.... you
> á áare writing a server-side program, right?
>
> á á--Michael
>
> á áOn 22 Oct, 2009, at 10:27 PM, Roan Brasil Monteiro wrote:
>
> á á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
> á á<mailto: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
> á á á á<mailto: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
> á á á á á á<mailto: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>
> á á á á á á á á á á<mailto: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/
>
>
>
>
>
>
>
> --
> Atenciosamente,
>
> Roan Brasil Monteiro
> http://roanbrasil.wordpress.com/
>
> ------------------------------
> Download Messenger onto your mobile for free. Learn more.<http://clk.atdmt.com/UKM/go/174426567/direct/01/>
> ------------------------------
> New Windows 7: Find the right PC for you. Learn more.<http://www.microsoft.com/windows/buy/>
>



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