I am having below null check Amount is having below fields
Amount{
Money amountToBePaid;
Float percentage;
}
and Money is having below fields:
Money {
String unit;
BigDecimal value
}
private boolean checkIfAmountDefined(Amount amount){
amountFlag=false;
If (null != amount || !ObjectUtils.isEmpty(amount)) {
if (!ObjectUtils.isEmpty(amount.getAmountToBePaid())&&
!ObjectUtils.isEmpty(amount.getPercentage())) {
if (!ObjectUtils.isEmpty(amount.getAmountToBePaid().getValue()) ||
!ObjectUtils.isEmpty(amount.getAmountToBePaid().getUnit())) {
amountFlag = true;
return amountFlag;
}
}
}
return amountFlag;
}
I need to check if the amount is not defined for which I am running below c check .The issue is I am getting null pointer exception for somtimes with percentage ,sometimes with other field. I can not ignore any of the inner fields as based upon these field I will consider the Amount is defined or not. Any suggestion ?
Your first if case If (null != Amount || !ObjectUtils.isEmpty(Amount))
should be
if (null != Amount && null != Amount.getAmountToBePaid() && !ObjectUtils.isEmpty(Amount))
as you use them later on, this ensures you won't get a null pointer exception
, as long as ObjectUtils.isEmpty(...)
handles null
appropriately.
If the method returns a boolean based on those conditionals, I'd return true;
if it manages through all of those if statements, return false;
at the end to fulfill your boolean
return
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.