java.lang.NullPointerException尝试在空对象引用上调用虚方法

时间:2018-05-31 05:32:41

标签: java android google-api

我有一些问题,当我想从谷歌地方细节api获取数据,我已经连接到api但这种情况我不明白 这是代码

ImageView image_map;
TextView opening_hours, place_address, place_name;

IGoogleAPIService mService;

PlaceDetail mPlace;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_views_places_map);

    mService = Common.getGoogleAPIService();

    image_map = (ImageView)findViewById(R.id.img_map);
    place_name = (TextView)findViewById(R.id.places_name);
    opening_hours = (TextView)findViewById(R.id.opening_hours);
    place_address = (TextView)findViewById(R.id.place_address);

    //Empty all view
    place_name.setText("");
    place_address.setText("");
    opening_hours.setText("");

    if (Common.currentResult.getPhotos() !=null && Common.currentResult.getPhotos().length > 0 )
    {
        Picasso.with(this)
                .load(getPhotoOfPlaces(Common.currentResult.getPhotos()[0].getPhoto_reference(),1000))
                .into(image_map);
    }

    if(Common.currentResult.getOpening_hours() != null )
    {
        opening_hours.setText("Buka Jam"+Common.currentResult.getOpening_hours().getOpen_now());
    }
    else
    {
        opening_hours.setVisibility(View.GONE);
    }

    //User service ngambil data address sama nama
    mService.getDetailPlace(getPlaceDetailUrl(Common.currentResult.getPlace_id()))
            .enqueue(new Callback<PlaceDetail>() {
                @Override
                public void onResponse(Call<PlaceDetail> call, Response<PlaceDetail> response) {

                    mPlace = response.body();
                    place_address.setText(mPlace.getResult().getFormatted_address());
                    place_name.setText(mPlace.getResult().getName());

                }

                @Override
                public void onFailure(Call<PlaceDetail> call, Throwable t) {

                }
            });
}

private String getPlaceDetailUrl(String place_id) {
    StringBuilder url = new StringBuilder("https://maps.googleapis.com/maps/api/place/details/json?");
    url.append("?placeid="+place_id);
    url.append("&key="+getResources().getString(R.string.browser_key));
    return url.toString();

}

private String getPhotoOfPlaces(String photo_reference, int maxWidth) {
    StringBuilder url = new StringBuilder("https://maps.googleapis.com/maps/api/place/photo");
    url.append("?maxwidth="+maxWidth);
    url.append("&photoreference="+photo_reference);
    url.append("&key="+getResources().getString(R.string.browser_key));
    return url.toString();
}

这是错误

  

BlockquoteProcess:com.example.novaldi.sabilulunganlaykes,PID:15422                     java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String com.example.novaldi.sabilulunganlaykes.Model.Result.getFormatted_address()'                         at com.example.novaldi.sabilulunganlaykes.ViewsPlacesMap $ 1.onResponse(ViewsPlacesMap.java:68)                         at retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run(ExecutorCallAdapterFactory.java:70)                         在android.os.Handler.handleCallback(Handler.java:789)                         在android.os.Handler.dispatchMessage(Handler.java:98)                         在android.os.Looper.loop(Looper.java:164)                         在android.app.ActivityThread.main(ActivityThread.java:6541)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)   E / UncaughtException:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String com.example.novaldi.sabilulunganlaykes.Model.Result.getFormatted_address()'                            at com.example.novaldi.sabilulunganlaykes.ViewsPlacesMap $ 1.onResponse(ViewsPlacesMap.java:68)                            at retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run(ExecutorCallAdapterFactory.java:70)                            在android.os.Handler.handleCallback(Handler.java:789)                            在android.os.Handler.dispatchMessage(Handler.java:98)                            在android.os.Looper.loop(Looper.java:164)                            在android.app.ActivityThread.main(ActivityThread.java:6541)                            at java.lang.reflect.Method.invoke(Native Method)                            在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

0 个答案:

没有答案