when i press the call driver button from the rider app. it shows the notifaction at driver app but it crashes because it gives error of invoking a nullpointer error. on a null object reference error comes at this line
(JSONObject jsonObject = new JSONObject(response.body().toString());)
here is my CustomerCall class
private void getDirection(double lat, double lng) {
String requestAPI = null;
try{
requestAPI = "https://maps.googleapis.com/map/api/directions/json?"+
"mode=driving&"+
"transit_routing_preference=less_driving&"+
"origin="+ Common.mLastLocation.getLatitude()+","+Common.mLastLocation.getLongitude()+"&"+
"destination="+lat+","+lng+"&"+
"key="+getResources().getString(R.string.google_direction_api);
Log.d( "Sunny",requestAPI ); //print url for debug
mService.getPath( requestAPI )
.enqueue( new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject jsonObject = new JSONObject(response.body().toString());
JSONArray routes = jsonObject.getJSONArray("routes");
//after get routes,just get 1st element route
JSONObject object = routes.getJSONObject(0);
//after get 1st element, we need array with name "legs"
JSONArray legs = object.getJSONArray("legs");
//and get 1st element of legs array
JSONObject legsObject = legs.getJSONObject(0);
//Now get Distance
JSONObject distance = legsObject.getJSONObject("distance");
txtDistance.setText(distance.getString("text"));
//get time
JSONObject time = legsObject.getJSONObject("duration");
txtTime.setText(time.getString("text"));
//get address
String address = legsObject.getString("end_address");
txtAddress.setText(address);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText( CustomerCall.this, ""+t.getMessage(), Toast.LENGTH_SHORT ).show();
}
} );
}catch (Exception e)
{
e.printStackTrace();
}
}
Logcat Error here
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shk.callingloader, PID: 12668
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
at com.example.shk.callingloader.CustomerCall$1.onResponse(CustomerCall.java:90)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)