添加到列表时的Nullpointerexception

时间:2013-06-08 17:57:05

标签: java list nullpointerexception

我正在尝试遍历一个对象列表,以确定哪些对象正在互相攻击。我使用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

    }

}

listContains class:

public boolean listContains(List<Gladiator> List, Gladiator search) {
    for (Gladiator p : List) {
        if (p.equals(search)) {
        return true;
        }
    }
    return false;

}

有谁知道为什么会这样?感谢

EDIT1:

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

我已经在类中初始化了list变量。

6 个答案:

答案 0 :(得分:4)

您忘记创建checkedGladiators对象。

因此,在循环之前创建对象:

列表与LT;争论者&GT; checkGladiators = new ArrayList&lt; Gladiators&gt;();

然后,在你的循环中,而不是测试checkGladiators == null ...

测试checkGladiators.isEmpty()。

答案 1 :(得分:3)

if(checkedGladiators ==null)是真的,你正在添加一些东西,

肯定会throw a NullPointerException,因为你在null

上运行

由于 ABHI

答案 2 :(得分:1)

为什么你需要做这一切?为什么这不够?

// This has your data in it.
List<Gladiators> gladiators = new ArrayList<Gladiators>();
// Obviously some attributes, including a unique key or name.  
// MUST override equals and hashcode properly
Gladiator g = new Gladiator();
if (gladiators.contains(g)) {
  // do something here.
}

NullPointerException是最容易解决的问题之一。在打开调试的IDE中运行代码,并在堆栈跟踪说明发生异常的位置放置一个断点。你会很快发现为什么你认为不应该为零的东西违反了你的假设。

答案 3 :(得分:0)

checkedGladiatorsnull时,您尝试添加它(就像它是List / Collection /等)。

...
if (checkedGladiators == null) {
    ...
    checkedGladiators.add(s);   // <-- You handle 'checkedGladiators'
                                // <-- as an instaciated, when it is 'null'

请改为:

...
if (checkedGladiators == null) {
    ...
    checkedGladiators = new ArrayList<...>();   // <-- instanciate appropriately
    checkedGladiators.add(s);  
    ...

答案 4 :(得分:0)

不是使用System.out.println进行核对,而是使用StackTracedebugger eclipse。我会明确指出。

  if (checkedGladiators == null) { 
            System.out.println("Null");
            combat1.add(s);
            checkedGladiators.add(s);  --> checkedGladiators is null here. Here null pointer exception will occur. 
            System.out.println("Null Changed"); 
        }

答案 5 :(得分:0)

检查checkedGladiators是否为null,然后在其上调用方法:

if (checkedGladiators == null) { // <-- null!!
   checkedGladiators.add(s); // <-- null pointer exception.
}