简体   繁体   English

添加到列表时出现Nullpointer异常

[英]Nullpointer exception when adding to list

I am trying to iterate through a list of objects to figure out which ones are fighting each other. 我正在尝试遍历对象列表,以找出哪些对象在互相影响。 I am using the checkedGladiators list as something to compare against to prevent it from checking gladiators already assigned to a combat, as each s that isn't assigned already will build their entire combat around them. 我现在用的是checkedGladiators列表的东西来比较,以防止它检查已经分配到战斗的角斗士,因为每个s即尚未分配将围绕它们构建其整个作战。 Currently I get a NullPointerException , so I used some test text to figure that it was happening right at listContains(checkedGladiators,s) . 当前,我得到一个NullPointerException ,因此我使用了一些测试文本来证明它listContains(checkedGladiators,s)发生在listContains(checkedGladiators,s) I added the part before it. 我在前面添加了零件。 NOW the problem is happening between "Null" and "Null Changed" as well, which makes no sense to me. 现在,“ 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: listContains类:

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

}

Does anyone know why this would occur? 有谁知道为什么会这样? Thanks 谢谢

Edit1: 编辑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

I have initialized the list variable in the class already. 我已经在类中初始化了列表变量。

You are forgetting to create the checkedGladiators object. 您忘记创建checkedGladiators对象。

So, create the object before your loop as: 因此,在循环之前创建对象为:

List<Gladiators> checkGladiators = new ArrayList<Gladiators>(); List <Gladiators> checkGladiators = new ArrayList <Gladiators>();

Then, in your loop, rather than testing for checkGladiators == null... 然后,在您的循环中,而不是测试checkGladiators == null ...

test for checkGladiators.isEmpty(). 测试checkGladiators.isEmpty()。

if(checkedGladiators ==null) is true and you are adding something to it , if(checkedGladiators ==null)为true,并且您要向其中添加一些内容,

it will definitely throw a NullPointerException , because you are operating on null 它肯定会throw a NullPointerException ,因为您对null进行操作

Thanks Abhi 谢谢阿比

Why do you need to do all this? 为什么需要做所有这一切? Why isn't this sufficient? 为什么这还不够?

// 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 is one of the easiest problems to fix. NullPointerException是最容易解决的问题之一。 Run your code in an IDE with debugging turned on and put a breakpoint where the stack trace says the exception occurred. 在调试打开的情况下在IDE中运行代码,并在堆栈跟踪表明发生异常的地方放置一个断点。 You'll figure out quickly why something you assumed should not be null has violated your assumptions. 您将很快找出为什么您认为不应为空的某些东西违反了您的假设。

When checkedGladiators is null you try to add to it (as if it were a List/Collection/etc). checkedGladiatorsnull您尝试将其添加(好像它是一个List / Collection / etc)。

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

Do this instead: 改为这样做:

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

Instead of checking with System.out.println , use StackTrace or a debugger like eclipse . 与其使用System.out.println进行检查, System.out.println使用StackTrace或类似eclipsedebugger I will pin-point clearly. 我会明确指出。

  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"); 
        }

you check if checkedGladiators is null and then call a method on it: 您检查checkGladiators是否为null,然后在其上调用一个方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM