方法抛出NPE但ArrayList中从不存在空值或空值

时间:2015-10-16 06:43:29

标签: java arraylist

这是抛出NPE的方法:

public void removeItemsbyBadWords() {
    String[] arrBadWords = settings.getBadWords();
    for(int i = 0;i < arrBadWords.length;++i) {
        String filter = ":contains("+arrBadWords[i]+")";
        Elements ele =  newsHeadlines.select(filter);
        if (ele != null && ele.size() > 0){
            for(Iterator<Element> iter = ele.iterator(); iter.hasNext();) {
               iter.remove();
            }
        }
    }
}

ArrayList ele被声明为对象变量,它由在此方法之前运行的方法初始化。我不知道为什么我的方法会抛出NPE。

这就是FindBugs关于我的方法的说法:

Bug: Read of unwritten field newsHeadlines in Client.removeItemsbyBadWords()

 The program is dereferencing a field that does not seem to ever have a non-null value written to it. Unless the field is initialized via some mechanism not seen by the analysis, dereferencing this value will generate a null pointer exception.  

Rank: Scary (8), 
confidence: Normal

Pattern: NP_UNWRITTEN_FIELD 

Type: NP, 
Category: CORRECTNESS (Correctness)<hr size="1" />

XML output:
<BugInstance type="NP_UNWRITTEN_FIELD" priority="2" rank="8" abbrev="NP" category="CORRECTNESS" first="25"> <Class classname="Client"> <SourceLine classname="Client" sourcefile="Client.java" sourcepath="Client.java"/> </Class> <Method classname="Client" name="removeItemsbyBadWords" signature="()V" isStatic="false"> <SourceLine classname="Client" start="146" end="156" startBytecode="0" endBytecode="276" sourcefile="Client.java" sourcepath="Client.java"/> </Method> <Field classname="Client" name="newsHeadlines" signature="Lorg/jsoup/select/Elements;" isStatic="false"> <SourceLine classname="Client" sourcefile="Client.java" sourcepath="Client.java"/> </Field> <SourceLine classname="Client" start="149" end="149" startBytecode="44" endBytecode="44" sourcefile="Client.java" sourcepath="Client.java"/> <SourceLine classname="Client" start="149" end="149" startBytecode="44" endBytecode="44" sourcefile="Client.java" sourcepath="Client.java"/> </BugInstance> <hr size="1" />

At Client.java:[line 149]
In method Client.removeItemsbyBadWords()
Field Client.newsHeadlines

我使用了我用于初始化ArrayList的方法的NPE,但这与使用迭代器来遍历列表并删除一些项目有关。

堆栈追踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Client.removeItemsbyBadWords(Client.java:149)
    at Client$1.actionPerformed(Client.java:68)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

2 个答案:

答案 0 :(得分:1)

FindBugs告诉您永远不会为newsHeadlines字段分配值。因此,调用newsHeadlines.select会导致NPE。

答案 1 :(得分:0)

问题出在for循环中,您总是尝试删除相同的元素。您必须在循环结束时调用iter.next(),将迭代器放在下一个元素上:

for(Iterator<Element> iter = ele.iterator(); iter.hasNext(); iter.next()) {
    iter.remove();
}