Hard to spot bugs
It took me a full 10 minutes to track down this stupid bug. It is not intuitive. I guess it is the penalty one pays for terseness:
vCostEntry.getCost().put(vDBField, Utils.makeProperString(vExportToExcel).equals("DISCOUNT")?((int)Utils.getDouble(vRawAmount)):GetIntFromFormattedCurrency(vRawAmount));
Some definitions to explain above:
public Map<String, Integer> getCost() {
return mCost;
}
private Integer GetIntFromFormattedCurrency(String pFormattedAmount) {...}
vCostEntry.getCost().put(vDBField, Utils.makeProperString(vExportToExcel).equals("DISCOUNT")?(Integer)((int)Utils.getDouble(vRawAmount)):GetIntFromFormattedCurrency(vRawAmount));
The bug is that the types of the values in the ternary operator ?: need to be consistent with the LHS type, but was not. In this case we tried to:
Integer <= int : Integer
which failed when GetIntFromFormattedCurrency(vRawAmount) returned a null, since Java will try to convert that to the explicit cast performed in the other part of the ternary operator - NOT very intuitive. By casting the first part of the ternary operator back to Integer, all works fine.
Read It Or Don't.
Add new comment