NullPointerException

View: New views
20 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

NullPointerException

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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/

Re: NullPointerException

by Michael Bar-Sinai :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can you post the stack trace? This might be something in the persistence setup

--Michael

On 22 Oct, 2009, at 9:43 PM, 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/


Re: NullPointerException

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

my stack trace is:
Do you need my persistence.xml or web.xml?

[#|2009-10-22T17:51:59.186-0200|SEVERE|sun-appserver2.1|javax.enterprise.system.container.web|_ThreadID=15;_ThreadName=httpSSLWorkerThread-8080-1;_RequestID=52240f11-5b0b-4e0f-b5a3-5b46896d4645;|StandardWrapperValve[Jersey Web Application]: PWC1406: Servlet.service() for servlet Jersey Web Application threw exception
java.lang.NullPointerException
    at com.citespace.citespacerest.dao.BaseDAO.ListCountry(BaseDAO.java:37)
    at com.citespace.citespacerest.UserResource.registerUser(UserResource.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:156)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:163)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:63)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:654)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:612)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:603)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:309)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:425)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:590)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:315)
    at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:288)
    at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:647)
    at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:579)
    at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831)
    at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
    at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
    at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
    at com.sun.enterprise.web.portunif.PortUnificationPipeline$PUTask.doTask(PortUnificationPipeline.java:380)
    at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
    at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
|#]


2009/10/22 Michael Bar-Sinai <mich.barsinai@...>
Can you post the stack trace? This might be something in the persistence setup

--Michael

On 22 Oct, 2009, at 9:43 PM, 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/

Re: NullPointerException

by Marina Vatkina :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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/


Re: NullPointerException

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: NullPointerException

by Marina Vatkina :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
> <mailto:Marina.Vatkina@...>>
>
>     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/


Re: NullPointerException

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>
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@... <mailto:Marina.Vatkina@...>>


   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/

Re: NullPointerException

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Chapter 24?

2009/10/22 Roan Brasil Monteiro <roanbrasil@...>
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@...>
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@... <mailto:Marina.Vatkina@...>>


   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/

Re: NullPointerException

by Marina Vatkina :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

All chapters under Persistence.

Roan Brasil Monteiro wrote:

> Chapter 24?
>
> 2009/10/22 Roan Brasil Monteiro <roanbrasil@...
> <mailto:roanbrasil@...>>
>
>     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@...
>     <mailto:Marina.Vatkina@...>>
>
>         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@...
>             <mailto:Marina.Vatkina@...>
>             <mailto:Marina.Vatkina@... <mailto:Marina.Vatkina@...>>>
>
>
>                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/


Re: NullPointerException

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Should I user @PersistenceContext EntityManager? to propagate to container all application?

2009/10/22 Marina Vatkina <Marina.Vatkina@...>
All chapters under Persistence.

Roan Brasil Monteiro wrote:
Chapter 24?

2009/10/22 Roan Brasil Monteiro <roanbrasil@... <mailto:roanbrasil@...>>


   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@...
   <mailto:Marina.Vatkina@...>>

       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@...
           <mailto:Marina.Vatkina@...>
           <mailto:Marina.Vatkina@... <mailto:Marina.Vatkina@...>>>



              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/

Re: NullPointerException

by Michael Bar-Sinai :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>
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@...>
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@... <mailto:Marina.Vatkina@...>>


   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/


Re: NullPointerException

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I keep getting NullPointerException. I am reading but over there is not telling me how to user in another layer. Please, someone can give me some help?

2009/10/22 Roan Brasil Monteiro <roanbrasil@...>
Should I user @PersistenceContext EntityManager? to propagate to container all application?


2009/10/22 Marina Vatkina <Marina.Vatkina@...>
All chapters under Persistence.

Roan Brasil Monteiro wrote:
Chapter 24?

2009/10/22 Roan Brasil Monteiro <roanbrasil@... <mailto:roanbrasil@...>>


   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@...
   <mailto:Marina.Vatkina@...>>

       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@...
           <mailto:Marina.Vatkina@...>
           <mailto:Marina.Vatkina@... <mailto:Marina.Vatkina@...>>>



              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/

Re: NullPointerException

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you Michael, I will try to implement it.

2009/10/22 Michael Bar-Sinai <mich.barsinai@...>
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@...>
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@...>
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@... <mailto:Marina.Vatkina@...>>


   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/

Re: NullPointerException

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>
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@...>
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@...>
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@... <mailto:Marina.Vatkina@...>>


   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/

Re: NullPointerException

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>
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@...>
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@...>
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@...>
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@... <mailto:Marina.Vatkina@...>>


   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/

Re: NullPointerException

by Michael Bar-Sinai :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>
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@...>
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@...>
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@... <mailto:Marina.Vatkina@...>>


   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/


Re: NullPointerException

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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@...>
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@...>
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@...>
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@...>
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@... <mailto:Marina.Vatkina@...>>


   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/

Re: NullPointerException

by Michael Bar-Sinai :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>
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@...>
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@...>
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@...>
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@... <mailto:Marina.Vatkina@...>>


   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/


Re: NullPointerException

by Marina Vatkina :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, it's better to use @PersistenceContext in the EJB.

-marina

Roan Brasil Monteiro wrote:

> Should I user @PersistenceContext EntityManager? to propagate to
> container all application?
>
> 2009/10/22 Marina Vatkina <Marina.Vatkina@...
> <mailto:Marina.Vatkina@...>>
>
>     All chapters under Persistence.
>
>     Roan Brasil Monteiro wrote:
>
>         Chapter 24?
>
>         2009/10/22 Roan Brasil Monteiro <roanbrasil@...
>         <mailto:roanbrasil@...> <mailto:roanbrasil@...
>         <mailto:roanbrasil@...>>>
>
>
>            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@...
>         <mailto:Marina.Vatkina@...>
>            <mailto:Marina.Vatkina@... <mailto:Marina.Vatkina@...>>>
>
>                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@...
>         <mailto:Marina.Vatkina@...>
>                    <mailto:Marina.Vatkina@...
>         <mailto:Marina.Vatkina@...>>
>                    <mailto:Marina.Vatkina@...
>         <mailto:Marina.Vatkina@...> <mailto:Marina.Vatkina@...
>         <mailto:Marina.Vatkina@...>>>>
>
>
>
>                       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/


Re: NullPointerException

by Michael Bar-Sinai :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>
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@...>
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@...>
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@...>
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@... <mailto:Marina.Vatkina@...>>


   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/

< Prev | 1 - 2 | Next >