| Forum | GitHub | Maps | Blog

Issues trying to route using GeoJSON



I am trying to use the GraphHopper core and JSON APIs(0.10.0 for both) to create a graph using GeoJSON and then do a routing. However I run into a couple of exceptions. Here is the relevant part of my code:

    gh = new GraphHopper().forMobile();

    ... other code (omitted) ...

    // This code runs in a background thread
    GHJsonFactory fac = new GHJsonFactory();
    GHJson json = fac.create();
    Reader reader = new FileReader(Environment.getExternalStorageDirectory().getAbsolutePath() + "/data.json");
    JsonFeatureCollection features = json.fromJson(reader, JsonFeatureCollection.class);


    GHRequest request = new GHRequest(51.05, -0.72, 51.0291, -0.7621).

    GHResponse resp = gh.route(request);
    PathWrapper pw = resp.getBest();

Initially I got this error:

IllegalArgumentException: To use the changeGraph API you need to turn off CH

I then added, immediately before changeGraph():


but now get:

NullPointerException: Attempt to invoke virtual method 'java.util.List com.graphhopper.routing.util.EncodingManager.fetchEdgeEncoders() on a null object reference

Looks like I’ve missed a step somewhere. The above code was based on my own examination of the GraphHopper source code. Any suggestions on this?



Can you send us minimal code that reproduces this NPE? An NPE should not be thrown even if the API is used in the ‘wrong’ order.

Disabling CH should happen before you call importOrLoad.


It’s on github:

(whole project

I don’t actually call importOrLoad() as I presumed the changeGraph() would import JSON. However if I do add it in I get a new exception (IllegalStateException: GraphHopperLocation not specified) - presumably because I haven’t loaded a .ghz file.



changeGraph changes an existing, previously imported graph it does not import GeoJSON. GeoJSON would be hard to handle, probably topojson would be a better possibility but it is not implemented.

What is the stacktrace of the NullPointerException? (update: ok, have identified&fixed this, the reason is the wrong assumption of the functionality and the missing import)


Ah right ok… sorry, my misunderstanding.