Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NullPointerException可以';我找不到错误_Java_Nullpointerexception_Dijkstra - Fatal编程技术网

Java NullPointerException可以';我找不到错误

Java NullPointerException可以';我找不到错误,java,nullpointerexception,dijkstra,Java,Nullpointerexception,Dijkstra,找不到获取此NullPointException的原因。它指向两条特定的线 这就是错误: Exception in thread "main" java.lang.NullPointerException at Railroad.dijkstra(Railroad.java:52) at Railroad.main(Railroad.java:36) 以下是两行: dijkstra(A); for (Edge x : w.edges){ 为了方便起见,以下是完整的代码: 发布整个代码,以便

找不到获取此NullPointException的原因。它指向两条特定的线

这就是错误:

Exception in thread "main" java.lang.NullPointerException
at Railroad.dijkstra(Railroad.java:52)
at Railroad.main(Railroad.java:36)
以下是两行:

dijkstra(A);

for (Edge x : w.edges){
为了方便起见,以下是完整的代码:

发布整个代码,以便更容易理解我的来历。希望这会有帮助,谢谢

    Vertex[] vertices = { A, B, C, D, E, F, G, H, I, J, K, L, M };
    dijkstra(A);
    for (Vertex v : vertices)
{
    System.out.println("Distance to " + v + ": " + v.shortestDist);
    List<Vertex> trip = cheapestTrip(v);
    System.out.println("Path: " + trip);
}
}

public static void dijkstra(Vertex s){
    s.shortestDist = 0;
    PriorityQueue<Vertex> cityQueue = new PriorityQueue<Vertex>();
    cityQueue.add(s);

    while(!cityQueue.isEmpty()){
        Vertex w = cityQueue.poll();
        for (Edge x : w.edges){
            Vertex v = x.city;
            int price = x.price;
            int priceOfTrip = w.shortestDist + price;
            if(priceOfTrip < v.shortestDist){   //relaxes the edge that it's on
                cityQueue.remove(v);
                v.shortestDist = priceOfTrip;
                v.prev = w;
                cityQueue.add(v);
            }
        }
    }

}
Vertex[]顶点={A,B,C,D,E,F,G,H,I,J,K,L,M};
迪克斯特拉(A);
对于(顶点v:顶点)
{
System.out.println(“到“+v+”的距离:“+v.shortestDist”);
列表行程=最便宜的行程(v);
System.out.println(“路径:+trip”);
}
}
公共静态空间dijkstra(顶点s){
s、 最短距离=0;
PriorityQueue cityQueue=新建PriorityQueue();
cityQueue.add(s);
而(!cityQueue.isEmpty()){
顶点w=cityQueue.poll();
对于(边x:w边){
顶点v=x.city;
int价格=x价格;
int priceOfTrip=w.最短距离+价格;
如果(priceOfTrip
您得到的是
NullPointerException
,因为
顶点
对象上的
字段未正确初始化。通常最好使用
private
字段和getter;这可能会引起有关潜在问题的警告

顶点
类中,应该初始化
。由于您没有发布代码,我们不知道它是什么类型,但是如果它是一个
集合
,例如,您会说:

Set<Edge> edges = Collections.emptySet(); // if you are going to replace edges
Set<Edge> edges = new HashSet<>();        // if you are going to use edges.add()
但最好重构到一个集合类型,这样可以添加任意数量的边,最好重构到不同类之间的字段封装


编辑2:具体问题在于
K
(DC)和
M
(NY)。您在其他城市设置了
字段,但在这些城市上没有设置。

您能指出铁路线吗。java:52在您的代码中吗?@AlexandreLavoie他这样做了。请发布
顶点
的代码。我怀疑某些
顶点
对象的
字段没有正确初始化。@chrylis是的,你是对的,我在考虑另一部分的代码。。。正如你所说,问题可能来自
PriorityQueue#poll()
。@AlexandreLavoie删除了这个答案,因为我没有对
while
子句给予足够的注意。OP正确地处理了队列,但我认为问题在于
w.edges.iterator()
。我刚刚发布了我的全部代码,也许这会让它更清晰。我敢肯定我做得对,除非我遗漏了我认为我是的东西。救生员,就是这样!谢谢!!:)哦,我没有在M(NY)上设置边缘的原因是因为这是最后一个节点/城市,所以它没有
Edge[] edges = new Edge[0];