gpt4 book ai didi

java - 运行 EJB 示例项目时出现错误 500 : java. lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 15:00:01 24 4
gpt4 key购买 nike

我是 EJB 新手,因此我正在执行教程 Java EE 6 tutorial 中的示例 EJB 项目。

这是 bean 的样子:

Converter.java

package converter.ejb;

import java.math.BigDecimal;
import javax.ejb.Remote;

@Remote
public interface Converter {
public BigDecimal dollarToYen(BigDecimal dollars);
public BigDecimal yenToEuro(BigDecimal yen);
}

ConverterBean.java

package converter.ejb;

import java.math.BigDecimal;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

@Stateless
public class ConverterBean {

private BigDecimal yenRate = new BigDecimal("115.3100");
private BigDecimal euroRate = new BigDecimal("0.0071");

public BigDecimal dollarToYen(BigDecimal dollars) {
BigDecimal result = dollars.multiply(yenRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}

public BigDecimal yenToEuro(BigDecimal yen) {
BigDecimal result = yen.multiply(euroRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
}

ConverterClient.java

package converter.ejb;

import java.math.BigDecimal;
import javax.ejb.EJB;

public class ConverterClient {

@EJB
private static Converter converter;

public ConverterClient(String[] args) {
}

public static void main(String[] args) {
ConverterClient client = new ConverterClient(args);
client.doConversion();
}

public void doConversion() {
try {
BigDecimal param = new BigDecimal("100.00");
BigDecimal yenAmount = converter.dollarToYen(param);

System.out.println("$" + param + " is " + yenAmount
+ " Yen.");
BigDecimal euroAmount = converter.yenToEuro(yenAmount);
System.out.println(yenAmount + " Yen is " + euroAmount
+ " Euro.");

System.exit(0);
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}

我有一个 Web 客户端,它是一个 JSP 页面:converterweb.jsp

<%@ page import="converter.ejb.Converter,
java.math.*, javax.naming.*"%>
<%!
private Converter converter = null;
public void jspInit() {
try {
InitialContext ic = new InitialContext();
converter = (Converter)
ic.lookup(Converter.class.getName());
} catch (Exception ex) {
System.out.println("Couldn’t create converter bean."+
ex.getMessage());
}
}
public void jspDestroy() {
converter = null;
}
%>
<html>
<head>
<title>Converter</title>
</head>

<body bgcolor="white">
<h1>Converter</h1>
<hr>
<p>Enter an amount to convert:</p>
<form method="get">
<input type="text" name="amount" size="25">
<br>
<p>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>

<%
String amount = request.getParameter("amount");
if ( amount != null && amount.length() > 0 ) {
BigDecimal d = new BigDecimal(amount);

BigDecimal yenAmount = converter.dollarToYen(d);
%>
<p>
<%= amount %> dollars are <%= yenAmount %> Yen.
<p>
<%
BigDecimal euroAmount =
converter.yenToEuro(yenAmount);
%>
<%= amount %> Yen are <%= euroAmount %> Euro.
<%
}
%>
</body>
</html>

但是每当我尝试执行此项目时,我都会收到 NullPointerException。我使用Websphere应用程序服务器。有人可以解释一下为什么会发生这种情况以及我该如何纠正它吗?

编辑:错误日志

[12/10/12 14:21:26:956 IST] 0000001f webapp        E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[/converterweb.jsp]: java.lang.NullPointerException
at com.ibm._jsp._converterweb._jspService(_converterweb.java:118)
at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1188)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:763)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:454)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
at com.ibm.wsspi.webcontainer.servlet.GenericServletWrapper.handleRequest(GenericServletWrapper.java:122)
at com.ibm.ws.jsp.webcontainerext.AbstractJSPExtensionServletWrapper.handleRequest(AbstractJSPExtensionServletWrapper.java:205)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1020)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:895)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1659)

最佳答案

您的代码充满错误:

  • EJB 依赖注入(inject)适用于容器管理的组件。您的 ConverterClient 上没有注释(我假设您没有在 xml 描述符中执行任何其他 EJB 配置)。所以容器不知道它并且无法进行注入(inject)。
  • 您的转换器被声明为静态的。这是行不通的。
  • 您正在尝试通过远程接口(interface)访问声明为 @LocalBean 的 bean(这意味着它公开无接口(interface) View )。这也行不通。尝试直接使用 ConverterBean 或避免使用 @LocalBean 并使用 @Remote(Converter.class) 代替。

也许我错过了什么。希望这会有所帮助。

关于java - 运行 EJB 示例项目时出现错误 500 : java. lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13797377/

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