潜在的空指针访问 [英] Potential null pointer access

查看:75
本文介绍了潜在的空指针访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种奇怪的情况,目前我尚不清楚:

I encounter a strange situation that is currently not that clear to me:

在Eclipse中启用潜在的空指针访问警告时,我收到如下警告(警告贴在相应注释之前的行):

When having the potential null pointer access warning enabled in Eclipse, I get warnings like in the following (the warnings are stick to the line preceding the corresponding comment):

protected Item findItemByName(String itemName, Items items) {
    boolean isItemNameMissing = null == itemName || itemName.isEmpty();
    boolean isItemsMissing = null == items || null == items.getItems() || items.getItems().isEmpty();

    if (isItemNameMissing || isItemsMissing) {
        return null;
    }
    // potential null pointer access: the variable items might be null at this location
    for (Item item : items.getItems()) {
        // potential null pointer access: the variable itemName might be null at this location
        if (itemName.equals(item.getName())) {
            return item;
        }
    }

    return null;
}

如果我检查 null,我也会遇到同样的情况使用Guava的前提条件,例如

The same happens to me if I check for null using Guava's Preconditions like

Preconditions.checkArgument(argument != null, "argument must not be null");

在哪里我可以理解,在后一种情况下,用于检查 IllegalArgumentException 可能会太困难/太昂贵或什至不可能发生,而我反过来也不明白为什么编译器会引发警告(如果我删除它们消失的检查)。

Where I can understand that in the latter case a flow analysis for checking when the IllegalArgumentException will happen might be too difficult/expensive or even impossible I in turn do not understand why the compiler raises the warning at all (if I remove the checks they disappear).

可以解释一下如何实现潜在的空指针访问以及为什么在两种情况下都将其引发吗?

Can one maybe explain how the potential null pointer access is accomplished and why it is raised in both of the cases? Or at least point me to the direction.

与此同时,我看一下是否自己找到了...

In the meanwhile I have a look and see whether I find it out myself...

附录

我已经将其分解为案例的核心部分。对于下面的类,警告仅显示在方法 sample2 中(再次由注释指出)。请注意,方法 sample3 也不触发警告。

I've kind of broken it down to the bare core of the case. Given the following class, the warning only shows up in the method sample2 (as pointed out by the comment again). Please note that the method sample3 does not trigger the warning either.

public class PotentialNullPointerAccess {

    public void sample1(final String aString) {

        if (aString == null) {
            return;
        }

        System.out.println(aString.length());
    }

    public void sample2(final String aString) {

        boolean stringIsNull = null == aString;

        if (stringIsNull) {
            return;
        }

        // Potential null pointer access: The variable aString might be null at this location
        System.out.println(aString.length());

    }


    public void sample3(final String aString) {

        System.out.println(aString.length());
    }
}


推荐答案

I认为Ed Merks在此论坛帖子中以某种方式回答了此问题:

I think it is somehow answered by Ed Merks within this forum post:

http://www.eclipse.org/forums/index.php/t/278687/

从据我了解,一旦您假设变量在前面的代码中可能为 null ,Eclipse就会发出警告。您可以通过检查 null (等于或不等于)来做到这一点-但您必须将其作为变量而不是简单地作为<$ c分开$ c>如果是唯一表达式。

From what I understand, Eclipse raises the warning once you assume a variable to potentially be null in the preceding code. You can do so by just check against null (either equal or not equal) - but you do have to have it separated somewhere as a variable, not simply as an if's solely expression.

这篇关于潜在的空指针访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆