我得到了带有嵌套异常的HttpStatus 500和java.lang.NullPointerException
我正在制作一个多货币转换器我曾经调试它到达这里时错误开始 currencypojo.setAmount(BigDecimal.valueOf(Integer.parseInt((request.getParameter(“txtAmount”))))); < / p>
MultiCurrencyController.java
package com.multicurrency;
import java.math.BigDecimal;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class MultiCurrencyController extends AbstractController{
private MultiCurrencyPOJO currencypojo;
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
currencypojo.setAmount(BigDecimal.valueOf(Integer.parseInt((request.getParameter("txtAmount")))));
currencypojo.setCurrency(request.getParameter("currency"));
return new ModelAndView ("home","CurrencyInfo",currencypojo);
}
public void multiCurrencyPOJO(MultiCurrencyPOJO currencypojo){
this.currencypojo=currencypojo;
}
}
MultiCurrencyPOJO.java
package com.multicurrency;
import java.math.BigDecimal;
public class MultiCurrencyPOJO {
private String currency;
private BigDecimal amount;
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public static BigDecimal getExchangeRate(String currency) {
return null;
}
}
答案 0 :(得分:0)
可能没有在HTTP请求中设置参数txtAmount
。为了更准确地说明错误发生的位置,我建议将其拆分为与类型转换的其余部分分开的一行,并明确检查它是否已设置。
例如:
String txtAmount = request.getParameter("txtAmount");
if (txtAmount == null) {
throw new IllegalArgumentException("Expected HTTP request parameter txtAmount not present");
}
currencypojo.setAmount(BigDecimal.valueOf(Integer.parseInt((txtAmount));
(另外,我首先要问BigDecimal
- 尤其是因为您要从Integer
转换它,但这不是{{1}}你问题的主题。)
答案 1 :(得分:0)
你在哪里MultiCurrencyPOJO
的对象
private MultiCurrencyPOJO currencypojo;
确保在对这些对象进行操作之前,您拥有此对象的实例并将其初始化。
默认情况下,私人objetcs
会初始化为null
,因此任何以null为单位的操作都将以NPE
结尾