使用ArrayList的NullPointerException - 应该不可能吗?

pim*_*eys 5 java

今天我得到了一个N​​ullPointerException,它实际上不会发生.

Exception in thread "Timer-9" java.lang.NullPointerException
    at packagename.censored.Bot.changeServergroups(Bot.java:1363)
    at packagename.censored.Bot.xpTask(Bot.java:1239)
    at packagename.censored.Bot.access$7(Bot.java:1187)
    at packagename.censored.Bot$9.run(Bot.java:729)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
Run Code Online (Sandbox Code Playgroud)

这是代码的相关部分:

public void changeServergroups(int cldbid, ArrayList<Integer> addList, ArrayList<Integer> removeList) {
    // If there are groups to add AND delete, remove them from the lists
    if (addList != null && removeList != null) {
        ArrayList<Integer> temp = new ArrayList<Integer>(addList);
        for (int s : temp) { // THIS IS LINE 1363
            if (removeList.contains(s)) {
                addList.remove((Integer) s);
                removeList.remove((Integer) s);
            }
        }
    }
    // some more code, to do the actual group changes
}
Run Code Online (Sandbox Code Playgroud)

怎么可能在那里得到NullPointerException?在从中addList创建新的临时ArrayList之前,我检查以确保它不为null.有人能告诉我这可能会在NullPointerException中返回吗?

ass*_*ias 9

唯一的可能是您的列表temp包含null.Integer然后将null 取消装箱int并抛出NPE.

如果可以for (Integer s : temp)接受空值,则可以使用NPE来解决问题.