Calculating total

Hi there,

Can anyone please help with the function below. Im trying to add some calculating function but the second part(where i’m trying to add) is being ignored.

Can anyone please help?

@Override
public void ready() {
evenementDs.addCollectionChangeListener(e -> getItem().setTotaleBijdrage(calculateTotaleBijdrage()));
}

private BigDecimal calculateTotaleBijdrage() {

    BigDecimal totaleBijdrage = BigDecimal.ZERO;

    if (getItem().getEvenement() != null)
                  
    totaleBijdrage = 
            getItem().getEvenement().stream()
                        .map(FundraisingEvenementItem::getKosten)
                        .reduce(BigDecimal.ZERO, BigDecimal::subtract);

            getItem().getEvenement().stream()
                        .map(FundraisingEvenementItem::getBijdrage)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
    

    return totaleBijdrage;
}

Hello fellow dutch speaker :smiley: .

your problem is not related to cuba but simply to java. You don’t do anything with the 2nd block. It’s not being ignored, you just don’t do anything with the result.

Further your null check isn’t followed by {} so if getEvenement() is null it will still blow up with a NullPointerExceptoin in the 2nd block.

Thanks for your Reply Tom. I’ve tried everything, also consulted JAVA topics on the internet, but i still can’t find the solution.

I’m trying to present the sum of the income and expense fields with my code. But as you can see the income fields are not added.

image

I’ve found the solution.

My working code:

@Override
public void ready() {
evenementDs.addCollectionChangeListener(e -> getItem().setEvenementSaldo(calculateEvenementSaldo()));
}

private BigDecimal calculateEvenementSaldo() {

    BigDecimal totaleBijdrage = ZERO;
    BigDecimal totaleKosten = ZERO;

    if (getItem().getEvenement() != null)
        totaleBijdrage =
                getItem().getEvenement().stream()
                        .map(FundraisingEvenementItem::getBijdrage)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

    if (getItem().getEvenement() != null)
        totaleKosten =
                getItem().getEvenement().stream()
                        .map(FundraisingEvenementItem::getKosten)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

    return totaleBijdrage.subtract(totaleKosten);
}