gpt4 book ai didi

java - 调用EJB方法时出现NullPointerException

转载 作者:行者123 更新时间:2023-12-02 13:28:10 26 4
gpt4 key购买 nike

我对 JavaEE 很陌生。我在这个网站和其他网站上查找了很多有关 EJB 注入(inject)的 NullPinterException 的问题,但还没有成功。我找不到问题的解决方案。

我在 Eclipse 中使用 JPA、JAX-RS、Glassfish Web Extensions 方面创建了 DynamicWebProject。这是我的项目的结构(web.xml 文件位于 glassfish-web.xml 文件下):

Project Structure

<小时/>

我尝试调用部署到本地主机 Glassfish 服务器的 gettemployees 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 和 session bean。我从 GetEmployees RESTful Web 服务类中创建了托管 bean。正确吗?

EmployeeEJB 是 session 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的注入(inject)不正确,但不知道如何赢得它。

最佳答案

我猜问题是您为 @ApplicationScoped 使用了错误的包。我认为您不需要 @ManagedBean

您正在使用:

import javax.faces.bean.ApplicationScoped;

尝试以下操作:

import javax.enterprise.context.ApplicationScoped;

另请参阅:

关于java - 调用EJB方法时出现NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43311424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com