java.lang.Nullpointerexception: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null Object Reference

Asked

Viewed 1,242 times

-1

I have a problem on line 141, where it says that the string is null and points out the error on this line JSONObject jsonObject = new JSONObject(response.body().toString());

However I am not able to solve, below the code along with the 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)

1 answer

0


You need to check if there is an object instance in your element response.body(), once you invoke a method, in case the toString(), that is not accessed in a static way.

One solution would be to include a minimum validation in your passage, 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);
    }
...
}
  • My friend made the following mistake: java.lang.Nosuchmethoderor: No Static method isNull(Ljava/lang/Object;)Z in class Ljava/util/Objects; or its super classes (declaration of 'java.util.Objects' appears in /system/framework/core-libart.jar)

  • Conflict problem with your dependency tree. I will edit the answer.

  • It worked my friend, the error disappeared and ran the project, thank you, if you have to consider something like let me know, it is the first time I request help here, vlwzao!!

  • Just accept the question as the solution to your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.