java.lang.NullPointerException:尝试调用虚拟方法';java.lang.String java.lang.String.toString()';关于空对象引用(帮助)

java.lang.NullPointerException:尝试调用虚拟方法';java.lang.String java.lang.String.toString()';关于空对象引用(帮助),java,android,json,nullpointerexception,response,Java,Android,Json,Nullpointerexception,Response,当我按下rider应用程序中的call driver按钮时。它在驱动程序应用程序上显示NotiAction,但它崩溃了,因为它给出了调用nullpointer错误的错误。 在空对象引用上,此行出现错误 (JSONObject JSONObject=newJSONObject(response.body().toString());) 这是我的客户呼叫课程 private void getDirection(double lat, double lng) { String requestA

当我按下rider应用程序中的call driver按钮时。它在驱动程序应用程序上显示NotiAction,但它崩溃了,因为它给出了调用nullpointer错误的错误。 在空对象引用上,此行出现错误

(JSONObject JSONObject=newJSONObject(response.body().toString());)

这是我的客户呼叫课程

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();
    }
}

错误消息显示:

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)
我不确定第90行是什么,但我想说response.body()为空,您正在调用它的toString()。在方法中添加一个空检查,它应该可以工作

您可以将代码打包到if语句中:

if(response.body() != null){
    //your try-catch block
}
或者,如果正文为空,则可以返回:

if(response.body() == null){
    return;
}
而不是

JSONObject jsonObject = new JSONObject(response.body().toString());
试用

JSONObject jsonObject = new JSONObject(response.body()); // remove .tostring()

CustomerCall.java中的第90行是什么?您的响应有正文吗?第90行是JSONObject JSONObject=newjsonobject(response.body().toString());对不起,我是android新手,你能指导我如何为这个添加空检查的语法吗?这与android无关。我编辑了我的答案。我使用了空检查。通过这种方式,应用程序不会崩溃,但当驾驶员应用程序收到通知时,仍然不会显示数据,并且不会显示时间、距离和地址。这意味着您没有任何数据可显示在通知中。你的问题出在别的地方。我试过了,但还是出现了同样的错误。这意味着你的回答。body()是空的,检查是否正确。你能告诉我怎么做吗?我是个新手,在过去的5天里我一直面临着这个问题。我使用了AndiCover告诉我的空支票,但就是这样。应用程序未崩溃,但通知发出时未显示任何数据
JSONObject jsonObject = new JSONObject(response.body()); // remove .tostring()