0

Hi i'm building a big project, using JGraphT. For this I have built a method that returns the shortest path between two sets of nodes, using Djikstra's class from JGraphT.

I get NullPointerException when I try to access path or it's submethods. This means

path = new DijkstraShortestPath(graph, v, y);

is ok, but

path.getPathLength()

throws the Exception.

This is the code:

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();
}

Any clues what can it be?

This is my system:

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)
serkef
  • 319
  • 2
  • 13
  • this is very good thought, but it's a constructor. It can't be null. can it? – serkef Jun 06 '13 at 19:49
  • I just looked at the DijkstraShortestPath code, it's not the case here. So I'm at a loss – greedybuddha Jun 06 '13 at 19:55
  • Is the graph object null ? And the CustomVertex ? Could you check that please ? – user2336315 Jun 06 '13 at 20:06
  • ok, seems like the problem is that there is no path. so this was the reason. Because object is not null, but the getPath method returns null. It seems like either it was matter of luck to bump in the wrong node, or some data get lost to their way to this function. – serkef Jun 06 '13 at 20:24
  • graph object is ok and contains all the vertices from the two sets. – serkef Jun 06 '13 at 20:28

1 Answers1

0

It seems that your graph isn't connected. If one of the nodes would be null, you would get a NullPointer even before evaluating the value of path length. You might want to try your case in a JUnit Test with a small connected sample graph and another which is not connected.

Matthias Kricke
  • 4,931
  • 4
  • 29
  • 43