0

I am writing an android project using themoviedatabase api (tmdb). I use this to get data from tmdb https://github.com/UweTrottmann/tmdb-java

And I use this library to set image to ImageView https://github.com/square/picasso

But when i run, it return null error at Picasso.get().load()

java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.uwetrottmann.tmdb2.entities.BaseMovie.backdrop_path' on a null object reference

@Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bindingView(view);
        bindingAction(view);

        // Create an instance of the service you wish to use
        // you should re-use these
        Tmdb tmdb = new Tmdb(API_KEY);
        MoviesService moviesService = tmdb.moviesService();

        // 438148   Minions
        // 299537    Captain Marvel
        //616037    Thor 4
        movie = loadMovieData(438148, tmdb, moviesService);

        Picasso.get().load("https://image.tmdb.org/t/p/original"+movie.backdrop_path).into(poster);

    }

public Movie loadMovieData(int movieId, Tmdb tmdb, MoviesService moviesService) {

        try {
            Response<Movie> response = moviesService
                    .summary(movieId, "en")
                    .execute();
            if (response.isSuccessful()) {
                Movie movie = response.body();
                return movie;
            }
        } catch (Exception e) {
            // see execute() javadoc
            e.printStackTrace();
            return null;
        }
        return null;
    }
  • 1
    Does this answer your question? [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) – David Jul 14 '22 at 17:04

1 Answers1

0

try it like this

if (response.isSuccessful()) {
        Movie movie = response.body();
        return movie;
        Picasso.get().load("https://image.tmdb.org/t/p/original"+movie.backdrop_path).into(poster);
    }

the cause is your method return null, but you should wait until response comes

Beant Singh
  • 169
  • 4