JavaNPE-关于摆脱它的帮助

JavaNPE-关于摆脱它的帮助,java,nullpointerexception,Java,Nullpointerexception,我目前得到了以下代码: 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(); to

我目前得到了以下代码:

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;
            }
        }
    }
以及:

任何帮助都将不胜感激,任何您可能需要的更多信息请告诉我


非常感谢。

因为
totalAmount
是可为空的,并且因为它在第一次迭代中使用,所以需要在循环之前将其设置为非
null
值:

totalAmount = 0L;
Map<String, Money> totals = captureFile.getTotalMoney();
for (Entry<String, Money> money : totals.entrySet()) {
    try {
        totalAmount = totalAmount + money.getValue().getEquivalent(BASE_CURRENCY).getAmount();
    }
    ... 
}
totalAmount=0L;
Map totals=captureFile.getTotalMoney();
对于(输入货币:totals.entrySet()){
试一试{
totalAmount=totalAmount+money.getValue().getEquivalent(基本货币).getAmount();
}
... 
}

如果在进入循环之前未将
totalAmount
设置为
null
,则第一个
+=
调用将导致NPE。

您似乎在说此行抛出NPE

totalAmount = totalAmount +
         money.getValue().getEquivalent(BASE_CURRENCY).getAmount();
有4种可能发生的方式:

  • 如果将
    totalAmount
    声明为
    Integer
    (或另一个基本包装类型),并且它是
    null

  • 如果
    money
    null

  • 如果
    money.getValue()
    返回
    null

  • 如果
    money.getValue().getEquivalent(基本货币)
    返回
    null

不幸的是,您的代码太零碎,无法判断哪些是可能的

也可能是
getEquivalent
正在抛出NPE。。。虽然从stacktrace上可以清楚地看到这一点

事实上,这条线:

totalAmount = totalMoney.getAmount();

不抛出NPE不允许我们消除上面列出的任何可能性。

你能发布你得到的堆栈跟踪吗?你可以更改totalAmount=null;to totalAmount=“”在catch块中,并试一试。如果循环的特定迭代引发异常,为什么要将totalAmount设置为null您从不检查变量的可空性。类似于
money.getValue().getEquivalent(基本货币).getAmount()
的东西可以很容易地生成NPE。@Andreas:java.lang.NullPointerException位于CaptureFileInsertOracle.init(CaptureFileInsertOracle.java:46)@KalairasanManimaran:因为我稍后会将它们传递给DB,如果它无法计算,我希望它被传递为null。这假设totalAmount是一个长或整数(我们需要看到它的声明才能确定)-另一种解释是money.getValue()返回null@tucuxi这里没有假设:
totalAmount
在第一个分支中分配
getAmount()
,OP显示
getAmount()
的签名,它返回
long
。在异常处理程序中,OP将
null
赋值给
totalAmount
,其类型必须是
Long
。如果我错了,请纠正我,但是为什么
Long totalAmount
(注意小写L)无法编译?为什么返回null的money.getValue()无法解释这些症状?@tucuxi如果
totalAmount
long
,而不是
long
,则异常处理程序的此赋值将不会编译:
totalAmount=null
money.getValue()
返回
null
肯定可以解释这些症状,但我认为这种可能性要小得多,因为OP在结果未知的情况下(比如,因为货币转换失败)使用异常而不是返回
null
s。哎哟-应该看到了。我在进一步寻找我的线索。
totalAmount = totalAmount +
         money.getValue().getEquivalent(BASE_CURRENCY).getAmount();
totalAmount = totalMoney.getAmount();