简体   繁体   English

PriorityQueue poll()抛出NullPointerException

[英]PriorityQueue poll() throwing NullPointerException

I am working with a priority queue in Java for the first time and I can't for the life of me understand what I am doing that is leading to the exception. 我是第一次使用Java处理优先级队列,我一生都无法理解我在做什么导致了异常。 I'm attempting to implement an ant colony type solution to the traveling salesman problem. 我正在尝试为旅行商问题实施一种蚁群式解决方案。 The following is the only code being called for my AntColony class. 以下是为我的AntColony类调用的唯一代码。

public AntColony(TSPInstance p) {
    PriorityQueue<Ant> ants = new PriorityQueue<Ant>(new AntComparator());
    size = p.getDimension();
    for (int i = 0; i < size; i++) {
        ants.offer(new Ant(p));
    }
    shortestTour = Integer.MAX_VALUE;
}

public void nextMove() {
    ants.poll();
}

The code that I'm running afterwards just as a test is as follows (just in a main method). 我随后作为测试运行的代码如下(仅在main方法中)。

AntColony a = new AntColony(p);
a.nextMove();

The a.nextMove() throws a NullPointerException at the ants.poll() part, but yet if I change the constructor to (for debugging purposes) a.nextMove()在ants.poll()部分抛出NullPointerException,但是如果我将构造函数更改为(出于调试目的)

public AntColony(TSPInstance p) {
    PriorityQueue<Ant> ants = new PriorityQueue<Ant>(new AntComparator());
    size = p.getDimension();
    for (int i = 0; i < size; i++) {
        ants.offer(new Ant(p));
    }
    ants.poll(); //ADDED THIS
    shortestTour = Integer.MAX_VALUE;
}

and then just do 然后做

AntColony a = new AntColony(p);

I don't get an exception. 我没有例外。 I'm struggling to understand how I'm getting an exception from ants.poll(), but yet when I call it from the constructor everything works. 我正在努力了解如何从ants.poll()获取异常,但是当我从构造函数调用它时,一切正常。 Any help with this would be appreciated. 任何帮助,将不胜感激。 There's a lot of code for various things in this project, so I didn't think uploading it all would help anybody so let me know if there's something I should include, but I don't see how the problem could lie outside these two bits of code. 这个项目中有很多用于处理各种事情的代码,所以我认为上传所有内容都不会对任何人有帮助,所以让我知道是否应该包含一些内容,但是我不认为问题可能出在这两个方面之外代码。

Added: Actual exception 补充:实际例外

Exception in thread "main" java.lang.NullPointerException
at data_structures.AntColony.nextMove(AntColony.java:25) (the ants.poll() part)
at algorithms.ACTest.main(ACTest.java:6) The a.nextMove() part

The ants variable in your AntColony constructor is a local variable. AntColony构造函数中的ants变量是局部变量。 So when you exit the constructor, it no longer exists. 因此,当您退出构造函数时,它不再存在。 Apparently the ants variable that your nextMove method is calling, is a class member. 显然, ants变量,你nextMove方法调用,是一类成员。

You need to change your constructor to have: 您需要将构造函数更改为:

    // initialize the class member, not a local instance.
    ants = new PriorityQueue<Ant>(new AntComparator());

You can just remove the PriorityQueue declaration in your AntColony constructor. 您可以只在AntColony构造函数中删除PriorityQueue声明。

public AntColony(TSPInstance p) {
    ants = new PriorityQueue<Ant>(new AntComparator());
    size = p.getDimension();
    ...
}

UPDATE: The cause for your NullPointerException is that you are not initializing your ants property in your constructor but you are creating a new local ants instead. 更新: NullPointerException的原因是您没有在构造函数中初始化ants属性,而是创建了一个新的本地ants So the ants object in nextMove method has the same value as you provided in your class level declaration, which it's probably null . 因此, nextMove方法中的ants对象具有与类级别声明中提供的值相同的值,该值可能为null

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

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