今天,我得到了一个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拿到那里呢?在创建一个新的临时addList之前,我会检查它是否为null。有人能告诉我如何在NullPointerException中返回吗?
发布于 2015-10-21 15:44:22
惟一的可能性是列表temp包含null。然后将空Integer解压缩到一个int,然后抛出一个NPE。
如果使用null值是可以接受的,则可以使用for (Integer s : temp)来解决NPE问题。
https://stackoverflow.com/questions/33263629
复制相似问题