0
Im新的Java和尝试一些财务分析Java文件我用于我的研究。当我在bean本身中创建一个主要方法时,我得到了它的工作。然而,当我尝试通过jsp形式传递所需的值并使用servlet doPost方法通过实例化bean中的方法来进行计算时,我什么也没有发生,我得到一个NullPointerException。我假设BlackScholes.java作为bean工作,并且问题出现在代码中。这里的帮助将不胜感激。使用Servlet doPost方法
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException {
String action = request.getParameter("action");
if (action != null) {
try {
String spotPrice = request.getParameter("sprice");
String strikePrice = request.getParameter("strike");
String interestRate = request.getParameter("rate");
String timeTo = request.getParameter("time");
String vol = request.getParameter("volatility");
double sprice = Double.parseDouble(spotPrice);
double strike = Double.parseDouble(strikePrice);
double rate = Double.parseDouble(interestRate);
double time = Double.parseDouble(timeTo);
double volatility = Double.parseDouble(vol);
double carryrate = rate;
request.setAttribute("sprice", sprice);
request.setAttribute("strike", strike);
request.setAttribute("rate", rate);
request.setAttribute("time", time);
request.setAttribute("volatility", volatility);
BlackScholes BS = new BlackScholes(carryrate);
BS.bscholEprice(sprice, strike, volatility, time, rate);
double currentpriceC = BS.getCalle();
request.setAttribute("validation1", currentpriceC);
} catch (NullPointerException e) {
return;
}
} else {
request.getRequestDispatcher("/index.jsp").forward(request,
response);
}
}
}
用来创造表单数据JSP文件:
<form action="${pageContext.request.contextPath}/OptionsController"
method="post">
<input type="hidden" name="action" value="docalculate" />
Spot Price: <input type="text" name="sprice" /> <br />
<p />
Strike Price: <input type="text" name="strike" /> <br />
<p />
Risk Free Rate: <input type="text" name="rate" /> <br />
<p />
Months To Expire: <input type="text" name="time" /> <br />
<p />
Volatility: <input type="text" name="volatility" /> <br />
<p />
<input type="submit" value="Submit" /><br />
<p />
One call options costs: <input type="text" name="validation1"
readonly="readonly"
value="<%if (request.getAttribute("validation1") != null) {
out.print(request.getAttribute("validation1"));
}%>">
</form>
使用'e.printStackTrace()'在catch块,在这里把异常的详细信息... – Prabhaker
作为@Prabhaker说,我们展示了异常的详细.. – user2520969
通过捕获NullPointerException异常,并在catch块返回仿佛什么都没有发生,你在脚下重新开枪:你隐藏了错误,无法找到它发生的地方。不要隐藏错误。修复它们。解决这个问题的第一件事就是删除这个catch块,让异常发生,然后读取异常堆栈跟踪。 –