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