String[] toppings = new String[10];
BigDecimal toppingsPrice = null;
toppings[0] = req.getParameter("extraCheese");
toppings[1] = req.getParameter("moreTomatoes");
toppings[2] = req.getParameter("extraOnions");
// ...
for(int i = 0; i < toppings.length; i++) {
if(toppings[i] != null) {
toppingsPrice.add(new BigDecimal("0.99")); // <-- NPE is caused here.
toppingsPrice = toppingsPrice.setScale(2, BigDecimal.ROUND_HALF_EVEN);
}
}
Я получаю NullPointerException
в приведенном выше коде при добавлении 0.99
к toppingsPrice
.Я работаю с денежными значениями, поэтому я использовал BigDecimal
.Что-то не так с тем, как я добавляю цену 0.99c?