Search code examples
jgrapht

DefaultEdge returning null for source and target for jgrapht


I am iterating over a set of edges returned from a jgrapht directed simple graph.

 Set<DefaultEdge> edges = graph.edgeSet();

    for (DefaultEdge edge : edges) {

       System.out.println(edge.getSource());
       System.out.println(edge.getTarget());

}

the edgeSet method returns the correct amount of edges, but the source and target are always null.


Solution

  • When I instantiated my graph and passed it an edge class:

    DirectedGraph graph = new SimpleDirectedGraph(DefaultEdge.class);
    

    Netbeans gave me the option for what DefaultEdge.class file to import, I chose the wrong one. I used the org.jgraph library instead of the org.jgrapht.

    If you are using the DefaultEdge class make sure you are using the one from jgrapht.

    import org.jgrapht.graph.DefaultEdge;
    

    otherwise there will be no compiler error, and everything will seem to work properly until there is an attempt to retrieve the edge data.