I want to calculate values from Java List. I managed to create the code using float in order to store money amount.
private float sumAmounts(List<ExpressCheckout> myList)
{
float total = 0.0f;
for (ExpressCheckout item : myList)
{
total += item.getAmount();
}
return total;
}
I tried this code:
private BigDecimal sumAmounts(List<ExpressCheckout> myList)
{
BigDecimal total = new BigDecimal(BigInteger.ZERO);
for (ExpressCheckout item : myList)
{
// total += item.getAmount();
total.add(item.getAmount());
}
return total;
}
But I get NPE when I execute the code. Is this a proper way to use BigDecimal?