0

I am trying to pull the release date value from TheMovieDatabase API. One of the items it returns is a Date value.

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeInt(vote_count);
    parcel.writeInt(id);
    parcel.writeByte((byte) (video ? 1:0));
    parcel.writeString(title);
    parcel.writeFloat(popularity);
    parcel.writeString(poster_path);
    parcel.writeString(original_language);
    parcel.writeString(original_title);
    parcel.writeStringList(genre_ids);
    parcel.writeString(backdrop_path);
    parcel.writeByte((byte) (adult ? 1:0));
    parcel.writeString(overview);
    parcel.writeLong(release_date.getTime());


}

IndividualMovieActivity.java:

public class IndividualMovieActivity extends AppCompatActivity {

private TextView mMovieTitle;
private ImageView mMoviePoster;
private TextView mMovieReleaseDate;

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

    Intent intent = getIntent();
    Movie movie = intent.getParcelableExtra("Movie");
    String title = movie.getOriginal_title();
    Log.v("POSTER_PATH", movie.getPoster_path());
    Log.v("RELEASE", movie.getRelease_date().toString());

    mMovieTitle = findViewById(R.id.movie_name);
    mMoviePoster = findViewById(R.id.movie_details_movie_poster_image);
    mMovieReleaseDate = findViewById(R.id.movie_details_release_date);

    mMovieTitle.setText(title);
    Picasso.with(getApplicationContext())
            .load("http://image.tmdb.org/t/p/w185" + movie.getPoster_path())
            .into(mMoviePoster);

    mMovieReleaseDate.setText((CharSequence) movie.getRelease_date());

   }
}

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.util.Date.toString()' on a null object reference at popularmovies.troychuinard.com.popularmovies.IndividualMovieActivity.onCreate(IndividualMovieActivity.java:29)

Full Source Code: https://github.com/troy21688/PopularMovies

tccpg288
  • 3,242
  • 5
  • 35
  • 80

3 Answers3

2

In your project's linked source code, these are the parcelable "read" and "write" methods:

protected Movie(Parcel in) {
    vote_count = in.readInt();
    id = in.readInt();
    video = in.readByte() != 0;
    title = in.readString();
    popularity = in.readFloat();
    poster_path = in.readString();
    original_language = in.readString();
    original_title = in.readString();
    genre_ids = in.createStringArrayList();
    backdrop_path = in.readString();
    adult = in.readByte() != 0;
    overview = in.readString();
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeInt(vote_count);
    parcel.writeInt(id);
    parcel.writeByte((byte) (video ? 1:0));
    parcel.writeString(title);
    parcel.writeFloat(popularity);
    parcel.writeString(poster_path);
    parcel.writeString(original_language);
    parcel.writeString(original_title);
    parcel.writeStringList(genre_ids);
    parcel.writeString(backdrop_path);
    parcel.writeByte((byte) (adult ? 1:0));
    parcel.writeString(overview);
    parcel.writeLong(release_date.getTime());
}

If you look carefully, you'll see that the Parcel constructor doesn't have a "read" operation for release_date. You probably need to add something like this:

release_date = new Date(in.readLong());
Ben P.
  • 52,661
  • 6
  • 95
  • 123
1

Looking at the source code, it doesn't look like the Movie(Parcel in) constructor ever initializes this.release_date, so it is null when you try to access it.

Tyler V
  • 9,694
  • 3
  • 26
  • 52
1

I have created a similar app called Cinemate using "TheMovieDatabase API" as a project, part of Udacity's Android Nanodegree program.

It appears to me, you may have a type problem. Try retrieving the date from the endpoint as a String and modify your Movie class accordingly.

Jantzilla
  • 638
  • 1
  • 7
  • 19