Save the coding, just don't allow null values in the database. Make the default value zero.
As for new BigDecimal(0): no, use BigDecimal.ZERO.
Save the coding, just don't allow null values in the database. Make the default value zero.
As for new BigDecimal(0): no, use BigDecimal.ZERO.
I had a similar problem (not related to a database though, just needed to sum up a couple of nullable BigDecimals). Did not find any library, so had to write the following function myself:
public static BigDecimal add(BigDecimal... addends) {
BigDecimal sum = BigDecimal.ZERO;
if (addends != null) {
for (BigDecimal addend : addends) {
if (addend == null) {
addend = BigDecimal.ZERO;
}
sum = sum.add(addend);
}
}
return sum;
}
The same in Java 8:
public static BigDecimal add(BigDecimal... addends) {
if (addends == null) {
return BigDecimal.ZERO;
}
return Arrays.stream(addends)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
Check out setParseBigDecimal in DecimalFormat. With this setter, parse will return a BigDecimal for you.
String value = "1,000,000,000.999999999999999";
BigDecimal money = new BigDecimal(value.replaceAll(",", ""));
System.out.println(money);
Full code to prove that no NumberFormatException is thrown:
import java.math.BigDecimal;
public class Tester {
public static void main(String[] args) {
// TODO Auto-generated method stub
String value = "1,000,000,000.999999999999999";
BigDecimal money = new BigDecimal(value.replaceAll(",", ""));
System.out.println(money);
}
}
Output
1000000000.999999999999999
If your main goal is validating BigDecimal dataType for nulls, then just make a comparison;
yourBigDecimal != null.
The above statement is enough for comparison and checking.
Your || should be an && - what's happening is that when you pass in a null value it's evaluating to false in the first two conditions, but then it's proceeding on to the third and fourth conditions and resulting in an exception. If you change the || to an && then short circuit evaluation will prevent the third and fourth conditions from evaluating.
Be certain to use a && and not a & - the former uses short-circuit evaluation, but the latter would force the third and fourth conditions to evaluate and you'll get a null pointer exception again. condition1 && condition2 says "return false if condition1 is false, else evaluate condition2" - if condition1 is false then condition2 is never evaluated. In contrast, condition1 & condition2 will always evaluate both conditions. The only reason to use & is if the conditions have side-effects. Likewise, condition1 || condition2 will not evaluate condition2 if condition1 is true, but | will always evaluate both conditions.
It's quite possible that caseXObj is null, so it'll cause the NullPointerException. You should test the three cases like this:
caseX caseXObj = caseXBo.getCaseXDao().findCaseXBySID(selectedID);
if (caseXObj != null && caseXObj.getCaseInGrossAmt() != null) {
// do something with caseXObj
} else if (caseXObj == null) {
// initialize caseXObj, you were misssing this case!
} else {
caseXObj.setCaseAmt(BigDecimal.ZERO);
}
In essence, the error was that you were testing for only two cases - and there are three of them.
Assuming it is OK for getCaseXDao() to return null, you need to assign to caseXObj rather than use it as a pointer in the else clause.
Build a comparator:
Comparator<BigDecimal> c = Comparator.nullsFirst(Comparator.naturalOrder());
(Or nullsLast, it doesn't matter if you're only ever comparing to zero).
Then:
if (c.compare(first, second) != 0) {
// ...
}
Try using
CompareToBuilder
class provided by
org.apache.commons.lang3
as it make null safe comparison. Sample code is:
public static <T, U> int nullSafeComparison(T t, U u) {
return new CompareToBuilder().append(t, u).toComparison();
}
public static void main(String[] args) {
BigDecimal zero = BigDecimal.ZERO;
BigDecimal zeroPzero = new BigDecimal("0.0");
System.out.println( zero + " " + zeroPzero);
System.out.println(nullSafeComparison(zero, zeroPzero));
}
If both numbers are same it will return 0, if 1st is greater than 2nd the result will be 1 and -1 if 1st number is less than 2nd.