Nullpointerexception in entitymanager
hy,
I'm having trouble in using entitymanager in a java web applicaton.
My code is:
public class HelloWorldResource extends ServerResource {
@PersistenceContext(unitName = "TestRestletPU")
EntityManagerFactory emf;
@Get
public String represent() {
EntityManager em = emf.createEntityManager();
if(em.isOpen())
return "good";
else
return "bad";
}
and my persistence.xml is
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns /persistence/persistence_2_0.xsd">
<persistence-unit name="TestRestletPU" transaction-type="JTA">
<jta-data-source>Test</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
All my entity classes and persistence.xml are auto generated, therefore i think there shouldn't be anything wrong.
I got everytime this error:
java.lang.NullPointerException
at firstSteps.HelloWorldResource.represent(HelloWorldResource.java:23)
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 org.restlet.resource.ServerResource.doHandle(ServerResource.java:449)
at org.restlet.resource.ServerResource.get(ServerResource.java:645)
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:527)
at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:587)
at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:299)
at org.restlet.resource.ServerResource.handle(ServerResource.java:846)
at org.restlet.resource.Finder.handle(Finder.java:510)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Router.doHandle(Router.java:497)
at org.restlet.routing.Router.handle(Router.java:737)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:151)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Filter.doHan开发者_开发百科dle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.engine.ChainHelper.handle(ChainHelper.java:111)
at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:72)
at org.restlet.Application.handle(Application.java:388)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Router.doHandle(Router.java:497)
at org.restlet.routing.Router.handle(Router.java:737)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Router.doHandle(Router.java:497)
at org.restlet.routing.Router.handle(Router.java:737)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:151)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.engine.ChainHelper.handle(ChainHelper.java:111)
at org.restlet.Component.handle(Component.java:388)
at org.restlet.Server.handle(Server.java:488)
at org.restlet.engine.http.connector.BaseServerHelper.handle(BaseServerHelper.java:158)
at org.restlet.engine.http.connector.BaseServerHelper.handleInbound(BaseServerHelper.java:167)
at org.restlet.engine.http.connector.BaseHelper.handleNextInbound(BaseHelper.java:418)
at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:695)
at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Can anyone help me out.
I think your class needs to be marked @Stateless or @Statefull to use @PersistenceContext
As far as I know, you can use either:
@PersistenceContext(unitName = "TestRestletPU") EntityManager em; (inside a stateless or statefull EJB session bean)
OR create your Entity Manager via the EntityManagerFactory, like you tried in your code with EntityManager em = emf.createEntityManager();
But you should not mix that code.
精彩评论