ClassCastException when querying the graph


First of all, million times sorry if I post in the wrong section.

I am currently upgrading my application using Graphhopper from 0.3 to 0.5.0.
Thanks to the release note and the “Low level API” explanations I could find my way through the API changes or almost.

Following the low-level-api (whith few tweaks because some methods’ signatures have changed) I ended up loading my graph and query it. But during the query the following exception raised :

java.lang.ClassCastException:$EdgeIterable cannot be cast to com.graphhopper.util.CHEdgeIteratorState
    at com.graphhopper.routing.util.LevelEdgeFilter.accept(
    at com.graphhopper.routing.AbstractRoutingAlgorithm.accept(
    at com.graphhopper.routing.DijkstraBidirectionRef.fillEdges(
    at com.graphhopper.routing.DijkstraBidirectionRef.fillEdgesTo(
    at com.graphhopper.routing.AbstractBidirAlgo.runAlgo(
    at com.graphhopper.routing.AbstractBidirAlgo.calcPath(
    at com.miprouting._live.LoadAndTest.main(

I can’t find why this occurs.
Here is the code creating the graph :

args = new String[]{

long start = System.currentTimeMillis();
CmdArgs cmd = new CmdArgs();
String[] keyVal;
for (String arg : args) {
    keyVal = arg.split("=");
    cmd.put(keyVal[0].trim(), keyVal[1].trim());

GraphHopper hopper = new GraphHopper();
System.out.println("Graph generated in " + (System.currentTimeMillis() - start) + "ms");

This seems to work fine as my graph is created, contracted and flushed.
(I deleted the previous graph, so it’s always created new)
Then here is how I try to query it :

String GRAPH_LOCATION = "/bel-car-fastest-gh";

FlagEncoder encoder = new CarFlagEncoder();
EncodingManager encodingManager = new EncodingManager(encoder);
Weighting weightCalc = new FastestWeighting(encoder);
GraphBuilder gb = new GraphBuilder(encodingManager)
GraphHopperStorage graph =;
LocationIndex index = new LocationIndexTree(graph, graph.getDirectory());

AlgorithmOptions options = AlgorithmOptions.start()
PrepareContractionHierarchies prepare = new PrepareContractionHierarchies(graph.getDirectory(), graph, graph.getGraph(CHGraph.class), encoder, weightCalc, TraversalMode.NODE_BASED);
QueryResult from = index.findClosest(51.005227, 3.885238, EdgeFilter.ALL_EDGES);
QueryResult to = index.findClosest(50.401629, 4.739425, EdgeFilter.ALL_EDGES);
QueryGraph queryGraph = new QueryGraph(graph);
queryGraph.lookup(from, to);
RoutingAlgorithm algo = prepare.createAlgo(queryGraph, options);

Path path = algo.calcPath(from.getClosestNode(), to.getClosestNode()); // <---- error occurs from here



What did I miss ?
Thank you already for the time you took reading, and I hope, helping me :smile:

Ok I got my mistake.

Pretty logical when I see the solution but not obvious at first glance.

The error is how I initialize the queryGraph.

It should rather be

QueryGraph queryGraph = new QueryGraph(graph.getGraph(CHGraph.class, weightCalc));

This works ! :smile:

Sorry for bothering you I was fighting this issue for a day :sweat_smile:

1 Like

At the first glance I was unsure too - but this now makes completely sense. We should probably throw an exception if we can detect this earlier … hmmh but not that easy as one can route on the normal graph too …

Thanks for question & solution!

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.