我已经初始化,但仍然出现空指针异常

时间:2018-09-25 15:23:13

标签: android

我收到此错误消息

  

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.util.List com.example.asus.cataloguemovieuiux.model.MovieResponse.getResults()'                         在com.example.asus.cataloguemovieuiux.fragment.PlayingFragment $ 1.onResponse(PlayingFragment.java:83)                         在retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run(ExecutorCallAdapterFactory.java:68)                         在android.os.Handler.handleCallback(Handler.java:739)                         在android.os.Handler.dispatchMessage(Handler.java:95)                         在android.os.Looper.loop(Looper.java:135)                         在android.app.ActivityThread.main(ActivityThread.java:5290)                         在java.lang.reflect.Method.invoke(本机方法)                         在java.lang.reflect.Method.invoke(Method.java:372)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:911)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)

我已经检查了Movie类,没有任何问题,还检查了MovieResponse类。我很困惑。为什么返回NullPointerException? 这是出现错误的方法代码:(

progressDialog = ProgressDialog.show(getActivity(), null, getResources().getString(R.string.loading_dialog), true, false);
    movieResponse = client.getInterface().getNowPlaying(BuildConfig.API_KEY);
    movieResponse.enqueue(new Callback<MovieResponse>() {
        @Override
        public void onResponse(Call<MovieResponse> call, Response<MovieResponse> response) {
            List<Movie> movies = response.body().getResults();
            mRecyclerView.setAdapter(new MoviesAdapter(getContext(), movies));
        }

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

        }
    });

在线

列出电影= response.body()。getResults();

2 个答案:

答案 0 :(得分:0)

当您尝试更改列表“电影”时,您会看到错误原因。检查response.body()。getResults()是否为空。

答案 1 :(得分:0)

尝试使用以下if语句包装代码,以检查是否为空;

if (response.body() != null){
    List<Movies> movies = response.body().getResults();
}