我對JavaEE非常陌生。我在本網站和其他網站上查詢了很多關於使用EJB注入的NullPinterException的問題,但還沒有成功。我無法找到解決問題的方案。調用EJB方法時出現NullPointerException
我在Eclipse中使用JPA,JAX-RS和Glassfish Web Extensions構建了DynamicWebProject。 這裏是我的項目的sctructure(web.xml文件是在GlassFish-web.xml文件下):
我嘗試調用它部署到本地主機GlassFish服務器,並嘗試裝getEmployees web服務從我的數據庫(也在本地主機)獲取有關所有員工的信息。 以下是我的GetEmployees類的代碼。
package bean;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import com.google.gson.Gson;
import ejb.EmployeeEJB;
@ApplicationScoped
@ManagedBean
@Path("/getemployees")
public class GetEmployees {
@Inject
EmployeeEJB employeeEJB;
@GET
@Produces("application/json; charset=utf-8")
public Response getAllEmployees() throws Exception {
Gson gson = new Gson();
String jsonString = null;
try {
jsonString = gson.toJson(employeeEJB.getAll());
} catch (Exception e) {
e.printStackTrace();
}
return Response.status(200).entity(jsonString).build();
}
}
我創建的託管和會話bean。我從我的GetEmployees RESTful webservice類中創建了Managed bean。這是對的嗎?
EmployeeEJB是會話bean:
package ejb;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import entity.Employee;
@LocalBean
@Stateless
public class EmployeeEJB {
@PersistenceContext
EntityManager em;
public void saveEmployee(Employee employee) {
em.merge(employee);
}
public void deleteEmployee(Employee employee) {
em.remove(employee);
}
public List<Employee> getAll() {
Query query = em.createQuery("SELECT emp FROM Employee emp", Employee.class);
List<Employee> emp = (List<Employee>) query.getResultList();
return emp;
}
}
這是我的persistence.xml文件。在Glassfish管理控制檯中創建JDBC連接池和JDBC資源。連接到數據庫是好的。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="FirstDynamicWebProject">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/second_attempt_hibernate</jta-data-source>
<class>entity.Docstatus</class>
<class>entity.Doctype</class>
<class>entity.Document</class>
<class>entity.Employee</class>
<class>entity.Mailorder</class>
<class>entity.Mailorderstatus</class>
</persistence-unit>
</persistence>
當我嘗試調用裝getEmployees類的employeeEJB.getAll()方法NullPointerException異常發生:
2017-04-10T00:59:07.063+0500|Severe: java.lang.NullPointerException
at bean.GetEmployees.getAllEmployees(GetEmployees.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
我覺得EmployeeEJB的注入是不正確的,但有沒有想法如何贏得它。
謝謝你的幫助,男人!我不認爲這會很容易!再次感謝! – Rinat
歡迎您:) – unwichtich
我剛剛嘗試將項目的war文件部署到另一個Glassfish服務器,該服務器安裝在遠程主機上,並在server.log中獲得相同的異常。我能做什麼?我嘗試清理項目,再次構建,再次清理構建和部署,但沒有任何幫助。你有什麼想法嗎? – Rinat