1

I am using retrofit 2 to query the TMDB api. In the call.enqueue method I am doing

List<Movie> movies = response.body.getMovies();

When I run, I get an error saying that the method getMovies produces a null pointer exception

API-INTERFACE

public interface ApiInterface {

@GET("/movie/now_playing")
Call<MainMovieNowPlayingResponse> getNowPlayingMovies(@Query("page")int 
page, @Query("api_key") String API_KEY);
}

Here is my ApiClient Class

public class ApiClient {

private static Retrofit retrofit = null;
public static Retrofit getClient(){
    if (retrofit == null){
        retrofit = new Retrofit.Builder()
                .baseUrl("https://api.themoviedb.org/3")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

    }
    return retrofit;
 }}

Here is the enqueue method im calling from the fragment

ApiInterface service = ApiClient.getClient().create(ApiInterface.class);
    Call<MainMovieNowPlayingResponse> call = service.getNowPlayingMovies(1,API_KEY);
    call.enqueue(new Callback<MainMovieNowPlayingResponse>() {
        @Override
        public void onResponse(Call<MainMovieNowPlayingResponse> call, Response<MainMovieNowPlayingResponse> response) {
            if (response.code() == 200 && response.isSuccessful()){
                movies = response.body().getResults();
            }
        }

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

        }
    });

I am calling this from the onCreateView method of a fragment which is a part of a tab view.

The error I'm getting is a NULL POINTER EXCEPTION in the response.body.getMovies() call. Where is my mistake??

P.S : I intentionally didn't place the Models because they were too long and I didn't want this question to be too lengthy. I have triple checked the models and all of them are right. All are private variables with public getters and setters.

And before you downvote this because this is yet another question of NullPointer , I know what a null pointer exception is. I just cant find the error in this code.

Where is my mistake?

Shreeya Chhatrala
  • 1,441
  • 18
  • 33
Sriram R
  • 2,109
  • 3
  • 23
  • 40

1 Answers1

1

I fixed it. There was a problem with the base url and the Api end points I had. The base url had to end with a / but instead i started my endpoint with a /

Sriram R
  • 2,109
  • 3
  • 23
  • 40