运行EJB示例项目时出现错误500:java.lang.NullPointerException
我是EJB新手,因此我执行了一个示例EJB项目,如教程中所示 以下是豆子的外观: Converter.java运行EJB示例项目时出现错误500:java.lang.NullPointerException,java,jsp,nullpointerexception,ejb,ejb-3.0,Java,Jsp,Nullpointerexception,Ejb,Ejb 3.0,我是EJB新手,因此我执行了一个示例EJB项目,如教程中所示 以下是豆子的外观: Converter.java package converter.ejb; import java.math.BigDecimal; import javax.ejb.Remote; @Remote public interface Converter { public BigDecimal dollarToYen(BigDecimal dollars); public BigDecimal y
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);
}
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);
}
}
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();
}
}
}
ConverterBean.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);
}
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);
}
}
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();
}
}
}
ConverterClient.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);
}
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);
}
}
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>
您的代码充满了错误:
- EJB依赖项注入适用于容器管理的组件。您的ConverterClient上没有注释(我假设您没有在xml描述符中进行任何额外的EJB配置)。所以容器并没有意识到这一点,也不能进行注射
- 您的转换器被声明为静态。这是行不通的
- 您正试图通过远程接口访问声明为@LocalBean的bean(这意味着它不公开任何接口视图)。这也行不通。尝试直接使用ConverterBean,或者避免使用@LocalBean,而是使用@Remote(Converter.class)
也许我错过了什么。希望这能有所帮助。您从哪里获得NPE?我假设在您调用
converter.dollarToYen(param)
的行中,对吗?@Yevgeniy我不确定NPE在哪里。Websphere错误日志指向Java文件,该文件是JSP文件的转换。我的JSP是converterweb.JSP,它指向converterweb.java的第118行,我无法打开该行。如果您不发布跟踪错误日志,很难“猜测”您的问题是什么。错误500表示您有服务器端错误。请粘贴在服务器日志中发现的错误。我想这不是OP所问问题的解释。@Andrey由于我是EJB新手,所以我一直遵循本教程,并尝试使用它@Rajath本教程中有一些要点与您的文章不同:-ConverterBean没有用@LocalBean;-ConverterBean实现转换器接口。“如果你修好了,我想它应该会工作的。”安德烈·波伦谢谢你指出它!已更正,但仍不起作用。@Rajath您是否根据更改更新了帖子?我这样问是因为您删除了LocalBean注释,但仍然没有使ConverterBean实现转换器。您应该在ConverterBean上添加额外的注释远程(Converter.class),或者让ConverterBean实现Converter。我想这可能是你问题的原因。