0

i got this error message

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.asus.cataloguemovieuiux.model.MovieResponse.getResults()' on a null object reference at com.example.asus.cataloguemovieuiux.fragment.PlayingFragment$1.onResponse(PlayingFragment.java:83) at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5290) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)

i have checked to Movie class and there's no something wrong, and also checked on MovieResponse class. i'm so confused. why it's return to NullPointerException? here's the code of method that got error :(

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) {

        }
    });

on line

List movies = response.body().getResults();

Mavisa9
  • 144
  • 4
  • 14
  • 3
    `response.body()` is null – leonardkraemer Sep 25 '18 at 15:28
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – leonardkraemer Sep 25 '18 at 15:30
  • yeah, check if the request was successful (response.isSuccessful()), nullity checks etc. Probably the API path is wrong, or the API key invalid. – Regulus Sep 25 '18 at 15:31
  • You might be getting null `response`, you can debug by putting breakpoint on that line and check if `response` is null. – Kinjal Rathod Sep 25 '18 at 15:33
  • why it's turn to null? – Mavisa9 Sep 25 '18 at 15:39
  • Probably your MovieResponse class does not map correctly the server's response. You can use this tool, for example: http://www.jsonschema2pojo.org/ to generate the class automatically. – Regulus Sep 25 '18 at 15:58

2 Answers2

0

You can see that your error is caused when you're trying to change your List 'movies'. Check to see if response.body().getResults() is null.

Joshua Best
  • 246
  • 1
  • 14
0

Try checking for null by wrapping your code with if statement as below;

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