私はJavaが初めてで、研究に使用している財務分析Javaファイルを試しています。Bean 自体の中でメイン メソッドを作成すると、動作するようになりました。ただし、jsp フォームを介して必要な値を渡そうとし、サーブレットの doPost メソッドを使用して、Bean からメソッドをインスタンス化することで計算を実行しようとすると、NullPointerException が発生します。BlackScholes.java が Bean のように機能し、問題が提示されたコード内にあると想定しています。ここで助けていただければ幸いです。
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>