English 中文(简体)
实体人员分类的点数例外
原标题:Nullpointerexception in entitymanager

hy

我在java网络复制场使用实体经理时遇到麻烦。

我的法典是:

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";
} 

页: 1

<?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> 

我的所有实体类别和持久性。 xml是自动生成的,因此,我认为有点错误。

我每次都会发现这一错误:

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

问题回答

我认为,你的班子需要打上“国家”或“国家”的标记,以便使用@PersistenceContext。

我知道,你可以使用:

@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();

但你不应混淆这一法典。





相关问题
Recommended way to develop using Jetty and Eclipse

I am currently developing a J2EE application and I would like to use Jetty. I would like to have iot integrated with Eclipse, so I could debug the appliaction. I ve tried out couple of plugins (...

Call function periodically in Java

we need run one function periodically in Java web application . How to call function of some class periodically ? Is there any way that call function when some event occured like high load in server ...

Why make an EJB rather than a Web Service?

I would have thought that there is a lot of information out there on this, but I haven t found anything that really answers my question. What are the advantages of making an EJB rather than a web ...

Where should I put this configuration setting?

I m designing a fairly small web application which will run on a Sun application server (v9.1). It only has a few pages, no database of its own, and will retrieve/update data via web services. There s ...

JNDI Names -- Is Prefix "jdbc/" needed?

What s up with JNDI names? I m trying to get a javax.sql.DataSource using the new annotations feature of Java 5. It s not working for me, so I want to ask... I have a in my web.xml, inside of it is ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

热门标签