0

I have this class:

public class Period {
private BigDecimal periodAmount;

public BigDecimal getPeriodAmount() {
    return periodAmount;
}

public void setPeriodAmount(BigDecimal periodAmount) {
    this.periodAmount = periodAmount;
}

And I was testing this on Main:

public static void main(String[] args){
    BigDecimal total = new BigDecimal(0);
    List<Period> periods = new ArrayList<Period>();

    Period periodOne = new Period();
    Period periodTwo = new Period();
    Period periodThree = new Period();
    Period periodFour = new Period();
    periodOne.setPeriodAmount(new BigDecimal(33564899.47));
    periodTwo.setPeriodAmount(new BigDecimal(22978517.87));
    periodThree.setPeriodAmount(new BigDecimal(19526899.32));
    periodOne.setPeriodAmount(new BigDecimal(18056899.98));

    periods.add(periodOne);
    periods.add(periodTwo);
    periods.add(periodThree);
    periods.add(periodFour);

    for(Period b : periods){
        total.add(b.getPeriodAmount());
    }
    System.out.println(total);
}

But it throws me a NullPointerException that I couldn't find the reason. I tried without a list, like this:

BigDecimal total = new BigDecimal(0);
Period periodOne = new Period();
periodOne.setPeriodAmount(new BigDecimal(33564899.47));
total.add(periodOne.getPeriodAmount());
System.out.println(total);

And then I get the total printed as 0. What am I doing wrong?

Felipe Wagner
  • 154
  • 2
  • 14

0 Answers0