Java org.graphstream.algorithm.APSP$APSPInfo.getShortestPathTo上的NullPointerException-图形问题

Java org.graphstream.algorithm.APSP$APSPInfo.getShortestPathTo上的NullPointerException-图形问题,java,graph,shortest-path,floyd-warshall,graphstream,Java,Graph,Shortest Path,Floyd Warshall,Graphstream,我正在开发一种算法来寻找两个机场之间的最佳路线。我偶然发现了图书馆的概念 内容是从文本文件中读取的。每行代表航空公司的中途停留: PEK,LAX,5 ATL,HND,75 ATL,LAX,20 DXB,HND,5 ATL,PEK,10 PEK,HND,40 LAX,DXB,20 ATL,DXB,55 因为这是一个涉及机票的问题,我把第三栏称为“价格”,但当我试图把真实的价格值放进去时,一些航线停止了工作。此外,其中一个功能是向文件中添加新行。我在为新中途停留添加任何值时遇到问题 在阅读中,我尝

我正在开发一种算法来寻找两个机场之间的最佳路线。我偶然发现了图书馆的概念

内容是从文本文件中读取的。每行代表航空公司的中途停留:

PEK,LAX,5
ATL,HND,75
ATL,LAX,20
DXB,HND,5
ATL,PEK,10
PEK,HND,40
LAX,DXB,20
ATL,DXB,55
因为这是一个涉及机票的问题,我把第三栏称为“价格”,但当我试图把真实的价格值放进去时,一些航线停止了工作。此外,其中一个功能是向文件中添加新行。我在为新中途停留添加任何值时遇到问题

在阅读中,我尝试将所有值除以最大权重,然后创建图形字符串,但没有成功:

String graphString;
String graphStringHeader = "DGS004\nmy 0 0\n";
String graphStringNodes = "";
String graphStringEdges = "";

BigDecimal biggestPrice = BigDecimal.ZERO;

while (stLines.hasMoreTokens()) {
    line = stLines.nextToken(System.getProperty("line.separator"));

    final StringTokenizer stColumns = new StringTokenizer(line);

    Airport originAux = null;
    Airport destinationAux = null;
    BigDecimal priceAux = null;

    String column;
    Integer columnIndex = 1;

    while (stColumns.hasMoreTokens()) {
        column = stColumns.nextToken(",");

        if (columnIndex == 1) {
            originAux = new Airport(column);

            if (!nodes.contains(column)) {
                // an = add node
                graphStringNodes += "an " + column + " \n";

                nodes.add(column);
            }
        } else if (columnIndex == 2) {
            destinationAux = new Airport(column);

            if (!nodes.contains(column)) {
                // an = add node
                graphStringNodes += "an " + column + " \n";

                nodes.add(column);
            }
        } else if (columnIndex == 3) {
            double parsedPreco = Double.parseDouble(column);
            priceAux = BigDecimal.valueOf(parsedPreco);

            /**
             * Normalizing values.
             */
            if (priceAux.intValue() > biggestPrice.intValue()) {
                biggestPrice = priceAux;
            } else {
                priceAux = priceAux.divide(biggestPrice, 2, RoundingMode.HALF_UP);
            }

            edges.add(originAux.getName() + "," + destinationAux.getName()+ "," + priceAux.intValue());

            stopovers.add(new Stopover(originAux, destinationAux, priceAux));

            // ae = add edge
            graphStringEdges += "ae " + originAux.getName() + "_" + destinationAux.getName() + " "
                    + originAux.getName() + " > " + destinationAux.getName()
                    + " price:" + priceAux.intValue()
                    + " \n";
        }

        columnIndex++;
    }

    lineIndex++;
}

graphString = graphStringHeader + graphStringNodes + graphStringEdges;
这就是引发的异常:

java.lang.NullPointerException: null
    at org.graphstream.graph.Path.add(Path.java:230)
    at org.graphstream.algorithm.APSP$APSPInfo.getShortestPathTo(APSP.java:594)
    at service.RouteFinderService.findRoute(RouteFinderService.java:183)
服务的第230行是:
Path shortestPath=edge.getShortestPathTo(destination.getName())
。该参数是目的地机场代码

我如何实例化
图形
APSP
对象:

Graph graph = new DefaultGraph("BestRouteGraph");
ByteArrayInputStream bs = new ByteArrayInputStream(graphString.getBytes());

FileSourceDGS source = new FileSourceDGS();
source.addSink(graph);

source.readAll(bs);

APSP apsp = new APSP();
apsp.init(graph);
apsp.setDirected(false);
apsp.setWeightAttributeName("price");

apsp.compute();
指在APSPInfo类中使用getAntivit方法

nodePath.push(edge.getOpposite(from));
确保检查是否存在指向节点的路径:

for(节点n:graph.getNodeSet()){
对于(节点n2:graph.getNodeSet()){
如果(n!=n2){
System.out.println(“在“+n+”和“+n2+”之间的最短路径:”;
试一试{
APSPInfo nodeInfo=n.getAttribute(“APSPInfo”);
System.out.println(nodeInfo.getShortestPathTo(n2.getId());
}捕获(例外e){
System.out.println(“无路径”);
}
}
}
}
例如,在您的数据集中,没有如图中所示的ATL路径

以下是您应该得到的结果:

Shortest Path between PEK and LAX :
[PEK, LAX]
Shortest Path between PEK and ATL :
no path
Shortest Path between PEK and HND :
[PEK, LAX, DXB, HND]
Shortest Path between PEK and DXB :
[PEK, LAX, DXB]
Shortest Path between LAX and PEK :
no path
Shortest Path between LAX and ATL :
no path
Shortest Path between LAX and HND :
[LAX, DXB, HND]
Shortest Path between LAX and DXB :
[LAX, DXB]
Shortest Path between ATL and PEK :
[ATL, PEK]
Shortest Path between ATL and LAX :
[ATL, PEK, LAX]
Shortest Path between ATL and HND :
[ATL, PEK, LAX, DXB, HND]
Shortest Path between ATL and DXB :
[ATL, PEK, LAX, DXB]
Shortest Path between HND and PEK :
no path
Shortest Path between HND and LAX :
no path
Shortest Path between HND and ATL :
no path
Shortest Path between HND and DXB :
no path
Shortest Path between DXB and PEK :
no path
Shortest Path between DXB and LAX :
no path
Shortest Path between DXB and ATL :
no path
Shortest Path between DXB and HND :
[DXB, HND]

看来这就是问题所在。我会在今天或明天晚些时候更好地测试它。是否有“正确”的权重值(如250作为PEK_ATL价格)或与图表无关?
Shortest Path between PEK and LAX :
[PEK, LAX]
Shortest Path between PEK and ATL :
no path
Shortest Path between PEK and HND :
[PEK, LAX, DXB, HND]
Shortest Path between PEK and DXB :
[PEK, LAX, DXB]
Shortest Path between LAX and PEK :
no path
Shortest Path between LAX and ATL :
no path
Shortest Path between LAX and HND :
[LAX, DXB, HND]
Shortest Path between LAX and DXB :
[LAX, DXB]
Shortest Path between ATL and PEK :
[ATL, PEK]
Shortest Path between ATL and LAX :
[ATL, PEK, LAX]
Shortest Path between ATL and HND :
[ATL, PEK, LAX, DXB, HND]
Shortest Path between ATL and DXB :
[ATL, PEK, LAX, DXB]
Shortest Path between HND and PEK :
no path
Shortest Path between HND and LAX :
no path
Shortest Path between HND and ATL :
no path
Shortest Path between HND and DXB :
no path
Shortest Path between DXB and PEK :
no path
Shortest Path between DXB and LAX :
no path
Shortest Path between DXB and ATL :
no path
Shortest Path between DXB and HND :
[DXB, HND]