0

This is my first question after many years of using this great forum and hence the first time I didn't find an answer to my issue :(

I'm getting a strange behaviour working with BigDecimal and Long: for the first one I’m not getting a NullPointerException but I do with the second.

My code is as following:

if (aggregate != null) {
    existingAggregates.setAmtExpect((aggregate.getAmtRecv());

    existingAggregates.setNbTrxExpect(aggregate.getNbTrxRecv());
}

Where aggregate and existingAggregates are of the same Object type Aggregate (I shortened the original class but in fact it is a Hibernate entity (if full version is needed, keep me posted :)

public class Aggregate implements Serializable {

    private BigDecimal amtExpect;

    private Long nbTrxExpect;

    public BigDecimal getAmtExpect() {
        return amtExpect;
    }

    public void setAmtExpect(BigDecimal amtExpect) {
        this.amtExpect = amtExpect;
    }

    public Long getNbTrxExpect() {
        return nbTrxExpect;
    }

    public void setNbTrxExpect(Long nbTrxExpect) {
       this.nbTrxExpect = nbTrxExpect;
    }

}

My problem happens when aggregate is not null, but when the two members amtExpect and nbTrxExpect are null.

Everything is doing fine with the getAmtRecv() but an NullPointerException is thrown with getNbTrxRecv().

I would understand if both would throw an NPE but I really don't get it why it only happens with the Long member :s

I searched the forum, but couldn't find any good explanation for me. Is it because of Autoboxing or something like that? I’m lost...

Anyway, To be able to run this code, I changed it like below:

if (aggregate != null) {
    existingAggregates.setAmtExpect((aggregate.getAmtRecv() != null) ? aggregate.getAmtRecv() : BigDecimal.ZERO);

    existingAggregates.setNbTrxExpect((aggregate.getNbTrxRecv() != null) ? aggregate.getNbTrxRecv() : BigDecimal.ZERO.longValue());
}

It's working, but I still don't get it what's going with the first version of my code.

Many thanks,
Noshitheel

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
  • Can you post the exception stack trace in here? I'm just curious as given with your current code, it should not happen. – kucing_terbang Oct 20 '15 at 10:17
  • 5
    Are you absolutely certain that it is `void setNbTrxExpect(Long nbTrxExpect)` and not `void setNbTrxExpect(long nbTrxExpect)`? I'd have guessed this was an auto-unboxing error. – Andy Turner Oct 20 '15 at 10:17
  • You seem to be deriving the long value from the BigDecimal, so why store the long value at all? Something is fishy here – fge Oct 20 '15 at 10:19
  • 1
    Are you sure that there are no fields/variables declared `long` anywhere, instead of `Long`? If so, that would kick in autounboxing and lead to the NPEs you're describing. (But as written I can't see why this would cause a problem.) – Andrzej Doyle Oct 20 '15 at 10:25
  • @kucing_terbang i tried to reproduce the case by putting back the old code and now it works :s – Noshitheel Oct 20 '15 at 10:49
  • @Andy Turner yes, i'm 100% sure there's no long used – Noshitheel Oct 20 '15 at 10:50
  • @fge i don't understand what you mean, could you please explain? thanks – Noshitheel Oct 20 '15 at 10:50
  • @Andrzej Doyle nope, i didn't used any long, but now i know if i do once i'll have to double check :) – Noshitheel Oct 20 '15 at 10:51
  • There must be something in the code that you've not posted, because there are only so many ways that `existingAggregates.setNbTrxExpect(aggregate.getNbTrxRecv());` can throw an NPE. – Andy Turner Oct 20 '15 at 10:54
  • Indeed @Andy Turner, or maybe i changed something since yesterday (yeah i spend half a day on this). Anyway, for safety, i will leave the double checks to prevent any exception. I've inherited that code, but for the next time, i'll took care of all you remarks – Noshitheel Oct 20 '15 at 11:08

0 Answers0