NullPointerException when adding to BigDecimal

Posted by Dan on Stack Overflow See other posts from Stack Overflow or by Dan
Published on 2012-03-25T17:13:43Z Indexed on 2012/03/25 17:30 UTC
Read the original article Hit count: 215

Filed under:
|
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);
    }
}

I am getting a NullPointerException in the above code when adding 0.99 to toppingsPrice. I am working with money values, so I have used BigDecimal. Is there something wrong with how I am adding the 0.99c price?

© Stack Overflow or respective owner

Related posts about java

Related posts about nullpointerexception