I am creating a sample app that sends notification to the driver when a rider requests pickup (like the uber app) I have a button for that functionality in the app.The app works fine and gets the driver but when i click on the button to call the driver the app crashes showing a nullpointerexception at if (response.body().success == 1) Toast.makeText(Home.this, "Request Failed!", Toast.LENGTH_SHORT).show();
this is the code
private void sendRequestToDriver(String driverId) {
DatabaseReference tokens = FirebaseDatabase.getInstance().getReference(Common.token_tbl);
tokens.orderByKey().equalTo(driverId)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
Token token = postSnapshot.getValue(Token.class);
String json_lat_lng = new Gson().toJson(new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude()));
Notification data = new Notification("SKYLAKE",json_lat_lng);
Sender content = null;
if (token != null) {
content = new Sender(token.getToken(),data);
}
mService.sendMessage(content)
.enqueue(new Callback<FCMResponse>() {
@Override
public void onResponse(@NonNull Call<FCMResponse> call, Response<FCMResponse> response) {
if (response.body().success == 1)
Toast.makeText(Home.this, "Request Failed!", Toast.LENGTH_SHORT).show();
else {
Toast.makeText(Home.this, "Request Sent!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<FCMResponse> call, Throwable t) {
Log.e("ERROR",t.getMessage());
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
ERROR LOG
03-23 16:46:35.606 18150-18150/com.joy.skylake.enlyftrider E/UncaughtException: java.lang.NullPointerException: Attempt to read from field 'int com.joy.skylake.enlyftrider.Model.FCMResponse.success' on a null object reference
at com.joy.skylake.enlyftrider.Home$3$1.onResponse(Home.java:192)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:762)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)