I’m trying to draw a Route Between A Box Image and the current Location on the Google map
I could diplay the Box image and the Current Location Icon but when I need to draw A route between them the App crash.
My Code:
private void drawRoute(LatLng yourLocation, String address) {
mService.getGeoCode(address).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject jsonObject = new JSONObject(response.body().toString());
String lat = ((JSONArray) jsonObject.get("results"))
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location")
.get("lat").toString();
String lng = ((JSONArray) jsonObject.get("results"))
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location")
.get("lng").toString();
LatLng orderLocation = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.box);
bitmap = Common.scaleBitmap(bitmap, 70, 70);
MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.title("Order of" + Common.currentRequest.getPhone())
.position(orderLocation);
mMap.addMarker(marker);
//draw route
mService.getDirections(yourLocation.latitude + "," + yourLocation.longitude,
orderLocation.latitude + "," + orderLocation.longitude)
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
new ParserTask().execute(response.body().toString());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
It Crash I think because it says that responses.body().toString() returns NULL.I Search A lot for A solution but didn’t know how to solve this issue.
The ERROR:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ymmyserver, PID: 32071
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object retrofit2.Response.body()' on a null object reference
at com.example.ymmyserver.TrackingOrder$1.onResponse(TrackingOrder.java:139)
The whole code on Github TrackingOrder Class:
https://github.com/zieadshabkalieh/YmmyApp/blob/main/TrackingOrder.java
I’m using this api:
package com.example.ymmyserver.Remote;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface IGeoCoordinates {
@GET("maps/api/geocode/json")
Call<String> getGeoCode(@Query("address") String address);
@GET("maps/api/directions/json")
Call<String> getDirections(@Query("origin") String origin, @Query("destination") String destination);
}
I think maybe the problem is that I’m using http not https maybe what’s the solutuon?
2
Answers
The error actually is that you can’t invoke
body()
on a NULL object, meaning yourresponses
variable itself is NULL. Which is because you have a typo in there, your parameter is calledresponse
, while you are trying to accessresponses
(mind the plural ‘s’ at the end).Janik is correct, the error stems from you trying to access a property on an object that doesn’t exist (NULL); I recommend play process of elimination by first debugging your API call, log the response (toString) to make sure first you are getting data, and that data does contain a body; follow up with the next part and so forth; sounds like the issue isn’t with the JSONObject method but that the data you’re consuming returns nothing (either nothing, or a different response than what you expect); debug it top to bottom you’ll find the reason.