import java.util.ArrayList;
public class Graph {
private Node[] nodes;
private int noOfNodes;
private Edge[] edges;
private int noOfEdges;
public Graph(Edge[] edges) {
this.edges = edges;
this.noOfNodes = calculateNoOfNodes(edges);
this.nodes = new Node[this.noOfNodes];
for (int n = 0; n < this.noOfEdges; n++) {
this.nodes[n] = new Node();
}
this.noOfEdges = edges.length;
----------------------错误------------------------ ----
for (int edgeToAdd = 0; edgeToAdd < this.noOfEdges; edgeToAdd++) {
this.nodes[edges[edgeToAdd].getFromNodeIndex()].getEdges().add(edges[edgeToAdd]);
this.nodes[edges[edgeToAdd].getToNodeIndex()].getEdges().add(edges[edgeToAdd]);
}
}
-------------------------------------------------------
Exception in thread "main" java.lang.NullPointerException
at com.repulo.dijsktra.Graph.<init>(Graph.java:25)
at com.repulo.dijsktra.Main.main(Main.java:22)
Blockquote
公共类主要{ 公共静态void main(String [] args){ Edge [] edge = { 新的Edge(0,2,1), 新的Edge(0,3,4), 新的Edge(0,4,2), 新的Edge(0,1,3), 新的Edge(1、3、2), 新的Edge(1、4、3), 新的Edge(1、5、1), 新的Edge(2,4,1), 新的Edge(3,5,4), 新的Edge(4,5,2), 新的Edge(4,6,7), 新的Edge(4,7,2), 新的Edge(5、6、4), 新Edge(6、7、5)
};
Graph g = new Graph(edges);
g.calculateShortestDistances();
g.printResult();
}
}
Blockquote
}