我正在尝试遍历对象列表以找出哪些对象在互相争斗。我使用该checkedGladiators
列表作为比较对象,以防止它检查已分配到战斗的角斗士,因为每个尚未分配的角斗士都s
将围绕他们进行整个战斗。目前我得到了一个NullPointerException
,所以我使用了一些测试文本来确定它发生在listContains(checkedGladiators,s)
. 我在它之前添加了部分。现在问题也发生在“Null”和“Null Changed”之间,这对我来说毫无意义。
for (Gladiator s : gladiators){
if (checkedGladiators == null) {
System.out.println("Null");
combat1.add(s);
checkedGladiators.add(s);
System.out.println("Null Changed");
}
if (listContains(checkedGladiators,s)) {
// if gladiator is already in a combat do nothing
} else { // if he isn't
}
}
列表包含类:
public boolean listContains(List<Gladiator> List, Gladiator search) {
for (Gladiator p : List) {
if (p.equals(search)) {
return true;
}
}
return false;
}
有谁知道为什么会发生这种情况?谢谢
编辑1:
public class Ai {
private List<Gladiator> gladiators;
private List<List<Gladiator>> combatsList;
private List<Gladiator> checkedGladiators;
private List<Gladiator> combat1;
private List<Gladiator> combat2;
private List<Gladiator> combat3;
private List<Gladiator> combat4;
private List<Gladiator> combat5;
private List<Gladiator> combat6;
private List<Gladiator> combat7;
private List<Gladiator> combat8;
private List<Gladiator> guardList;
private List<Gladiator> advanceList;
private List<Gladiator> retreatList;
int totalCombats = 0; // total combats going on
我已经在类中初始化了列表变量。