今天我在实际无法发生的地方遇到了 NullPointerException。
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)
这是代码的相关部分:
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
}
怎么可能在那里得到 NullPointerException?在从中创建新的临时 ArrayList 之前,我检查以确保 addList
不为 null。有人能告诉我这怎么可能返回 NullPointerException 吗?
最佳答案
唯一的可能性是您的列表 temp
包含 null
。然后将 null Integer
拆箱为 int
并抛出 NPE。
如果可以接受空值,您可以使用 for (Integer s : temp)
解决 NPE。
关于java - ArrayList 的 NullPointerException - 应该不可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33263629/