我有这个简单的EJB类。当我尝试注入EntityManager
个实例时,我得到一个NullPointerException
:
Caused by: java.lang.NullPointerException
at pl.take.server.model.WholesaleEJB.createClient(WholesaleEJB.java:54) [:]
问题是我做错了什么,我该如何解决?我正在使用netbeans和jboss 6.1。谢谢你的时间。
@Stateless
public class WholesaleEJB {
@PersistenceContext(unitName = "wholesale")
EntityManager entityManager;
public void createClient(Clients client) {
entityManager.persist(client); //Error line
}
}
的persistence.xml
<?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="wholesale" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/Wholesale</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
编辑:
具有WholesaleEJB实例的REST类:
@Stateless
@Path("/wholesale")
public class WholesaleREST{
@EJB
private WholesaleEJB wholesaleEJB = new WholesaleEJB();
@POST
@Path("/create")
public String createClient(InputStream is) {
Clients clients = JAXB.unmarshal(is, Clients.class);
wholesaleEJB.createClient(clients);
return "Client created!";
}
}
完整错误:
00:31:56,458 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/WholesaleApp].[default]] Servlet.service() for servlet default threw exception: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
at org.jboss.resteasy.core.SynchronousDispatcher.unwrapException(SynchronousDispatcher.java:345) [:6.1.0.Final]
at org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:321) [:6.1.0.Final]
at org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:214) [:6.1.0.Final]
at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190) [:6.1.0.Final]
at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:534) [:6.1.0.Final]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:496) [:6.1.0.Final]
at org.jboss.resteasy.core.SynchronousDispatcher.invokePropagateNotFound(SynchronousDispatcher.java:155) [:6.1.0.Final]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:212) [:6.1.0.Final]
at org.jboss.resteasy.plugins.server.servlet.FilterDispatcher.doFilter(FilterDispatcher.java:59) [:6.1.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) [:6.1.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.1.0.Final]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.1.0.Final]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [:6.1.0.Final]
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.1.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.1.0.Final]
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.1.0.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159) [:6.1.0.Final]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.1.0.Final]
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.1.0.Final]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.1.0.Final]
at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.1.0.Final]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.1.0.Final]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.1.0.Final]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.1.0.Final]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.1.0.Final]
at java.lang.Thread.run(Thread.java:745) [:1.8.0_51]
Caused by: java.lang.NullPointerException
at pl.take.server.model.WholesaleEJB.createClient(WholesaleEJB.java:48) [:]
at pl.take.server.model.WholesaleREST.createClient(WholesaleREST.java:57) [:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.8.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [:1.8.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [:1.8.0_51]
at java.lang.reflect.Method.invoke(Method.java:497) [:1.8.0_51]
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:140) [:6.1.0.Final]
at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:255) [:6.1.0.Final]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:220) [:6.1.0.Final]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:209) [:6.1.0.Final]
at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:519) [:6.1.0.Final]
... 23 more
来自服务器上的deploing项目的完整日志: http://pastebin.com/Ki3Gz4RA
答案 0 :(得分:0)
这是你的问题:
@EJB
private WholesaleEJB wholesaleEJB = new WholesaleEJB();
通过调用构造函数,您正在构建wholesaleEJB
的POJO。 EJB的工作原理是允许容器创建一个远程代理,它围绕POJO包装EJB服务(包括依赖注入)。
将其替换为:
@EJB
private WholesaleEJB wholesaleEJB;
然后容器将读取注释,注入EJB的副本,并准备好调用它。如果你想自己控制进程(你不应该这样做),你可以使用JNDI查找来获取EJB对象,而不是直接调用构造函数。
答案 1 :(得分:0)
可能您的Application Server不符合EJB 3.1,并且您无法以这种方式执行EJB注入。
您可以尝试使用所需的方法为EJB创建一个接口,然后在REST服务中注入此接口。