|
View:
New views
20 Messages
—
Rating Filter:
Alert me
|
| < Prev | 1 - 2 | Next > |
|
|
NullPointerExceptionI 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: NullPointerExceptionCan 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? |
|
|
Re: NullPointerExceptionmy 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@...>
-- Atenciosamente, Roan Brasil Monteiro http://roanbrasil.wordpress.com/ |
|
|
Re: NullPointerExceptionYou 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: NullPointerExceptionHow 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. -- Atenciosamente, Roan Brasil Monteiro http://roanbrasil.wordpress.com/ |
|
|
Re: NullPointerExceptionIt 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: NullPointerExceptionJust 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? -- Atenciosamente, Roan Brasil Monteiro http://roanbrasil.wordpress.com/ |
|
|
Re: NullPointerExceptionChapter 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. -- Atenciosamente, Roan Brasil Monteiro http://roanbrasil.wordpress.com/ |
|
|
Re: NullPointerExceptionAll 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: NullPointerExceptionShould I user @PersistenceContext EntityManager? to propagate to container all application?
2009/10/22 Marina Vatkina <Marina.Vatkina@...> All chapters under Persistence. -- Atenciosamente, Roan Brasil Monteiro http://roanbrasil.wordpress.com/ |
|
|
Re: NullPointerExceptionYou 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? |
|
|
Re: NullPointerExceptionI 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? -- Atenciosamente, Roan Brasil Monteiro http://roanbrasil.wordpress.com/ |
|
|
Re: NullPointerExceptionThank you Michael, I will try to implement it.
2009/10/22 Michael Bar-Sinai <mich.barsinai@...>
-- Atenciosamente, Roan Brasil Monteiro http://roanbrasil.wordpress.com/ |
|
|
Re: NullPointerExceptionOne 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@...>
-- Atenciosamente, Roan Brasil Monteiro http://roanbrasil.wordpress.com/ |
|
|
Re: NullPointerExceptionI 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: -- Atenciosamente, Roan Brasil Monteiro http://roanbrasil.wordpress.com/ |
|
|
Re: NullPointerExceptionYes, 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: |
|
|
Re: NullPointerExceptionit'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@...>
-- Atenciosamente, Roan Brasil Monteiro http://roanbrasil.wordpress.com/ |
|
|
Re: NullPointerExceptionAre 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? |
|
|
Re: NullPointerExceptionYes, 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: NullPointerExceptionAh! 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:
|
| < Prev | 1 - 2 | Next > |
| Free embeddable forum powered by Nabble | Forum Help |