-1

I do not know why the response.body() is null, that make my app crashing, check the code please

This is Common Class

public class Common {
public static User userterkini;
public static tujuan tujuanterkini;

public static final String UPDATE = "Update";
public static final String DELETE = "Delete";
public static final int PICK_IMAGE_DI_PESAN=71;
public static final String baseUrl = "https://maps.googleapis.com";

public static kordint getgeokodeservis(){
    return retrofitclient.getClient(baseUrl).create(kordint.class);
}
public static Bitmap scalebitmap(Bitmap bitmap,int lebar,int tinggi)
{
    Bitmap scalebitmaps = Bitmap.createBitmap(lebar,tinggi,Bitmap.Config.ARGB_8888);
    float scaleX = lebar/(float)bitmap.getWidth();
    float scaleY = tinggi/(float)bitmap.getHeight();
    float pivotX=0,pivotY=0;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(scaleX,scaleY,pivotX,pivotY);

    Canvas canvas=new Canvas(scalebitmaps);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap,0,0,new Paint(Paint.FILTER_BITMAP_FLAG));

    return scalebitmaps;
}
}

this is kordnt Interface

package com.example.wsulangserver01.Remote;


public interface kordint {
@GET("maps/api/geocode/json")
Call<String> getgeokode(@Query("address")String address);

@GET("maps/api/directions/json")
Call<String>gettujuan(@Query("origin")String origin,@Query("destination")String destination);
}

this is retrofitclient class

public class retrofitclient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl)
{
    if (retrofit==null)
    {
        retrofit=new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }
    return retrofit;
}
}

and this is the maps activity

private void rute(final LatLng lokasiuser, String alamat) {
    kordint.getgeokode(alamat).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("geometri")
                        .getJSONObject("location")
                        .get("lng").toString();

                String lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                        .getJSONObject("geometri")
                        .getJSONObject("location")
                        .get("lng").toString();

                LatLng lokasipesanan = new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));


                Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.wahidin);
                bitmap = Common.scalebitmap(bitmap,70,70);

                MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
                        .title("Dikirim ke "+Common.tujuanterkini.getNotelp())
                        .position(lokasipesanan);
                mMap.addMarker(marker);

                //jalur rute
                kordint.gettujuan(lokasiuser.latitude+","+lokasiuser.longitude,lokasipesanan
                +","+lokasipesanan.latitude+","+lokasipesanan.longitude).enqueue(new Callback<String>() {
                    @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                        new parser().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) {

        }
    });
    }

and Bellow is the error

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String 
 java.lang.String.toString()' on a null object reference
    at com.example.wsulangserver01.Mapps$1.onResponse(Mapps.java:126)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7397)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:920)

I do not know what is happening on my response.body() that why always null, please help me to fix this :)

  • 1
    It may be worthwhile to debug your code in your IDE and you can see where exactly the body of your response is hitting a NPE. – Oozeerally Dec 24 '19 at 14:20

2 Answers2

0

Retrofit onResponse (documentation) is invoked when an HTTP response is received, in other words, 200 or 404 also considered. Please use Response.isSuccessful() to check if the request succeeds, otherwise, just print the error code and/or error body to debug.

Mehdi Sakout
  • 773
  • 5
  • 8
-1

You can create a Model based on your JSON response, after that use Gson and GsonConverter!! when You use GsonConverter, it is not necessary to parse JSON , because of Gson do this for your code!!