Potential null pointer access

别来无恙 提交于 2019-12-07 18:04:43

问题


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

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;
}

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

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

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...

Addendum

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());
    }
}

回答1:


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

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

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.




回答2:


This is probably not the cause of the warning but here you can have a null pointer.

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;
    }
}

You iterate over objects, but the object returned can be null. So item.getName() can cause a null pointer exception.

Exmaple

 List<String> l = new ArrayList<String>();
 l.add("test");
 l.add(null);
 l.add("another string");

 if(l == null)   // <-- this is similar to the above check
    return;

 for(String s : l)
 {
     s.charAt(0);   //  <-- null pointer access on second item.
 }


来源:https://stackoverflow.com/questions/16687820/potential-null-pointer-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!