大家好,我正在使用JGraphT构建一个大项目。为此,我使用JGraphT中的Djikstra类构建了一个方法,该方法返回两组节点之间的最短路径。
当我试图访问path或它的子方法时,我得到了NullPointerException。这意味着
path = new DijkstraShortestPath(graph, v, y);是可以的,但是
path.getPathLength()引发异常。
代码如下:
static GraphPath GraphsDistance(Graph graph, Set<CustomVertex> source, Set<CustomVertex> destination){
//returns the shortest path between 2 sets on nodes
DijkstraShortestPath path,solution;
double distance=Double.MAX_VALUE;
solution=null;
for(CustomVertex v:source){
for(CustomVertex y:destination){
path = new DijkstraShortestPath(graph, v, y);
if (path.getPathLength()<distance){
distance=path.getPathLength();
solution=path;
}
}
}
System.out.println("source: "+source.toString()+ "\ndestination: "+destination.toString());
return solution.getPath();
}有什么线索可能是什么?
这是我的系统:
Product Version: NetBeans IDE 7.0.1 (Build 20121011-unknown-revn)
Java: 1.6.0_27; OpenJDK Client VM 20.0-b12
System: Linux version 3.5.0-17-generic running on i386; UTF-8; en_US (nb)发布于 2013-06-08 18:39:45
看起来你的图表没有连接。如果其中一个节点为空,则甚至在计算路径长度的值之前就会得到一个NullPointer。您可能希望在JUnit测试中使用一个小的连接示例图和另一个未连接的示例图来尝试您的情况。
https://stackoverflow.com/questions/16970729
复制相似问题