DirectionResult has null routes and waypoints - Google Directions API
<p>My goal for this program was to poll the Google Directions API and plot the course by Polyline on a MapView in an Android app.</p>

<p>However, when I get the <code>DirectionsResult</code> back from the API call, trying to access <code>directionsResult.routes[0]</code> or <code>directionsResult.geocodedWaypoints[0]</code> results in a <strong>NullPointerException</strong>.</p>

<p>I am currently importing the following libraries using maven repositories (excerpt from my build.gradle):</p>

<pre><code>compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.http-client:google-http-client-android:1.21.0'
compile 'com.google.http-client:google-http-client-jackson:1.21.0'
compile 'com.google.http-client:google-http-client-jackson2:1.21.0'
compile 'com.google.http-client:google-http-client-gson:1.21.0'
compile 'com.google.http-client:google-http-client-protobuf:1.21.0'
compile 'com.google.http-client:google-http-client:1.21.0'
compile 'com.google.maps:google-maps-services:0.1.10'
compile 'com.google.android.gms:play-services:8.4.0'
</code></pre>

<p>My current code inside an AsyncTask implementation with debugging calls commented out:</p>

<pre><code>@Override
protected synchronized String doInBackground(URL... params)
{
    try {
        HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
            @Override
            public void initialize(HttpRequest request) throws IOException {
                request.setParser(new JsonObjectParser(JSON_FACTORY));
            }
        });

        GenericUrl url = new GenericUrl("http://maps.googleapis.com/maps/api/directions/json");
        // to and from are both LatLng's
        url.put("origin", from.toUrlValue());
        url.put("destination", to.toUrlValue());
        url.put("sensor", true);
        HttpRequest request = requestFactory.buildGetRequest(url);
        //wait(1000);
        HttpResponse httpResponse = request.execute();
        //Log.i(TAG, httpResponse.parseAsString());
        //wait(1000);
        DirectionsResult directionsResult = httpResponse.parseAs(DirectionsResult.class);
        //wait(1000);
        //Log.i(TAG, Integer.toString(httpResponse.getStatusCode()));
        //Log.i(TAG, httpResponse.getStatusMessage());
        latlngs = directionsResult.routes[0].overviewPolyline.decodePath();

    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
</code></pre>

<h2>My Debugging Process:</h2>

<ol>
<li><p>This is my printed request url:</p>

<p><a href="http://maps.googleapis.com/maps/api/directions/json?origin=40.426870,-86.925083&amp;destination=40.430092,-86.921679&amp;sensor=true" rel="nofollow">http://maps.googleapis.com/maps/api/directions/json?origin=40.426870,-86.925083&amp;destination=40.430092,-86.921679&amp;sensor=true</a></p></li>
</ol>

<p>Obviously it is a short but complete call. With elements of legs containing polyline points (which is what I assume I'm after). This JSON is the same as the response I got from reading <code>httpResponse.parseAsString()</code>.</p>

<ol start="2">
<li><p>Looking at some StackExchange questions, I saw some people suggested waiting for data to be received. I put 1 second delays between all integral parts of request cycle with no results.</p></li>
<li><p>None of the other parts of the call were null.</p></li>
<li><p><code>httpResponse.getStatusCode()</code> returned 200.</p></li>
<li><p><code>httpResponse.getStatusMessage()</code> returned OK.</p></li>
</ol>

<p>Everything appears normal until I attempt to use <code>DirectionsResult</code> to parse the JSON <code>HttpResponse</code> with:</p>

<pre><code>DirectionsResult directionsResult = HttpResponse.parseAs(DirectionsResult.class);
</code></pre>

<p>After which I get a NullPointerException when accessing the two fields of <code>DirectionsResult</code>: <code>routes</code> and <code>geocodedWaypoints</code>.</p>

<p>Has anyone had similar issues with this class? I've been thinking I might submit an issue on the proper google-http-client library GitHub page if this isn't resolved here.</p>