java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString ()' on a null object reference

-1

I'm having trouble on line 141, where it says the string is null and points me to the error in this line JSONObject jsonObject = new JSONObject(response.body().toString());

But I am not able to solve, below the code along with logcat :

try {
        JSONObject jsonObject = new JSONObject(response.body().toString());

    String lat = ((JSONArray)jsonObject.get("results"))
                  .getJSONObject(0)
                  .getJSONObject("geometria")
                  .getJSONObject("localizacao")
                  .get("lat").toString();

    String lng = ((JSONArray)jsonObject.get("results"))
                  .getJSONObject(0)
                  .getJSONObject("geometria")
                  .getJSONObject("localizacao")
                  .get("lng").toString();

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

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.box);
    bitmap = Comum.scaleBitmap(bitmap, 70, 70);

    MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
                .title("Pedido de "+Comum.pedidoAtual.getTelefone())
                .position(localizacaoPedido);
    mMap.addMarker(marker);

ERROR:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
    at com.example.daniel.androidpadariaserver.RastreamentoPedido$1.onResponse(RastreamentoPedido.java:141)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    
asked by anonymous 12.04.2018 / 16:35

1 answer

0

You need to check for an instance of an object in your response.body() element, since you invoke a method, in this case toString() , which is not accessed in a static way.

One solution would be to include a minimal validation in your excerpt, as in the following example:

try {
    if(response.body() == null) {
        //Faz o tratamento para quando não existir o elemento body
    } else {
        JSONObject jsonObject = new JSONObject(response.body().toString());

        String lat = ((JSONArray)jsonObject.get("results"))
              .getJSONObject(0)
              .getJSONObject("geometria")
              .getJSONObject("localizacao")
              .get("lat").toString();

        String lng = ((JSONArray)jsonObject.get("results"))
              .getJSONObject(0)
              .getJSONObject("geometria")
              .getJSONObject("localizacao")
              .get("lng").toString();

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

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.box);
        bitmap = Comum.scaleBitmap(bitmap, 70, 70);

        MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
            .title("Pedido de "+Comum.pedidoAtual.getTelefone())
            .position(localizacaoPedido);
        mMap.addMarker(marker);
    }
...
}
    
12.04.2018 / 19:21