mercredi 28 janvier 2015

Null pointer exception thrown when creating a Double to test null check

This may be a stupid question, but I have a method that will convert Doubles or BigDecimals to a formatted String:



//There is also a method that accepts BigDecimals
public String convertToCurrencyString(Double aAmount) {
if(aAmount == null){
return "$0.00";
}
NumberFormat numFormat = DecimalFormat.getCurrencyInstance(Locale.US);
numFormat.setMinimumFractionDigits(2);
String amount = numFormat.format(aAmount);
return amount;
}


I'm currently trying to test this method with JUnit by instantiating a Double:



Double nullDouble = new Double(null);


The problem is a NullPointerException is being thrown when the creating the Double. Is there a way to stop this happening, or is this normal behaviour? The need for the null check is that there are some older records in a database that have these Doubles and BigDecimals set to null


Aucun commentaire:

Enregistrer un commentaire