I currently got the following piece of code:
captureFile = theCaptureFile;
// If there is only 1 currency it gets the total right away
if (captureFile.getTotalMoney().size() == 1) {
Money totalMoney = captureFile.getTotalMoney().values().iterator().next();
totalAmount = totalMoney.getAmount();
currencyCode = totalMoney.getCurrencyCode();
}
// If there is more than one currency, goes through every one, converts it to GBP and adds it to the total amount
else if (captureFile.getTotalMoney().size() > 1) {
Map<String, Money> totals = captureFile.getTotalMoney();
for (Entry<String, Money> money : totals.entrySet()) {
try {
totalAmount = totalAmount + money.getValue().getEquivalent(BASE_CURRENCY).getAmount();
}
catch (CurrencyNotFoundException e) {
LOG.error("Getting ExchangeRate:", e);
totalAmount = null;
break;
}
catch (ExchangeRateNotFoundException e) {
LOG.error("Getting ExchangeRate:", e);
totalAmount = null;
break;
}
}
}
When the code gets called, the first IF works just fine but, in case there are more than 2 currencies, I get a NullPointerException on the TRY bit. None of those methods have a return null so I'm guessing it is something wrong that I am doing on the mapping part to pull the values out.
Here are the other 2 methods that are pulled off:
public Money getEquivalent(String targetCurrencyCode) throws CurrencyNotFoundException,ExchangeRateNotFoundException {
if (this.currencyCode.equals(targetCurrencyCode)) {
return this;
}
return getEquivalent(CurrencyCache.get().getCurrency(targetCurrencyCode));
}
and:
public long getAmount() {
return amount;
}
Any help would be greatly appreciated, any more info you might need just let me know.
Many thanks in advance.