-1

I have never used butterknife before. Instead of using the repeated findViewById(...) in my project, I want to use that library instead. However I get an exception.

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                            at popularmovies.theo.tziomakas.popularmovies.DetailsFragment$1.onResponse(DetailsFragment.java:224)

Here is my class.

public class DetailsFragment extends Fragment implements
                LoaderManager.LoaderCallbacks<Object>,
                TrailersAdapter.TrailersAdapterOnClickHandler{
            private static final int TRAILERS_LOADER_ID = 0;
            private static final int REVIEWS_LOADER_ID =   1;

            private static final String TRAILERS_LAYOUT = "DetailsFragment.trailer.layout";
            private static final String REVIEWS_LAYOUT = "DetailsFragment.review.layout";

            int recyclerViewOrientation = LinearLayoutManager.VERTICAL;

            private static final String TRAILERS_LIST = "trailers_list";
            private static final String REVIEWS_LIST = "reviews_list";
            /*****************************************************
             * Lists where fetched trailers and reviews are stored.
             *****************************************************/
            private List<Trailers> trailersList = new ArrayList<>();
            private List<Reviews> reviewsList = new ArrayList<>();

            private static final String[] FAVOURITE_MOVIE_PROJECTION = {
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_ID,
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_POSTER_PATH,
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_TITLE,
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_OVERVIEW,
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_VOTE_AVERAGE,
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_RELEASE_DATE
            };

            /**************************************************************************
             * Those custom adapter will actually send the views to the recycler views.
             **************************************************************************/
            private  TrailersAdapter trailersAdapter;
            private  ReviewsAdapter reviewsAdapter;

            /*******************************************
             * RecyclerViews that will display our data.
             *******************************************/
            private  RecyclerView trailersRecyclerView;
            private  RecyclerView reviewsRecyclerView;


            private static final String MOVIE_SHARE_HASHTAG = " #PopularMoviesApp";
            private static final String LOG_TAG = "DetailsFragment";

            private Cursor favoriteCursor;
            private String mMovieId;
            private String mImage;
            private String mTitle;
            private double mRating;
            private String mDate;
            private String mOverview;
            private String oldReviewText;

            @BindView(R.id.no_reviews_text_view)
            public TextView noReviewTextView;

            @BindView(R.id.no_trailers_text_view)
            public TextView noTrailersTextView;

            @BindView(R.id.movie_image)
            public ImageView i;

            @BindView(R.id.movie_title)
            public TextView t;

            @BindView(R.id.movie_rating)
            public TextView r;

            @BindView(R.id.movie_date)
            public TextView d;

            @BindView(R.id.movie_overview)
            public TextView o;
            ...
            ButterKnife.bind(getActivity());
             Call<Movies> call = apiService.getMovieDetails(mMovieId, BuildConfig.MOVIESDB_API_KEY);
    call.enqueue(new Callback<Movies>() {
        @Override
        public void onResponse(Call<Movies> call, Response<Movies> response) {
            Movies movie = response.body();
            mImage = movie.getImageThumbnail();
            mTitle = movie.getTitle();
            mOverview = movie.getOverview();
            mRating = movie.getRating();
            mDate = movie.getDate();

            Picasso.with(getContext()).load(NetworkUtils.IMAGE_URL + NetworkUtils.IMAGE_SIZE_185 + mImage).into(i);
            //I get the error here. 
            t.setText(mTitle);
            o.setText(mOverview);
            r.setText(String.valueOf(mRating));
            d.setText(mDate);

        }

        @Override
        public void onFailure(Call<Movies> call, Throwable t) {
            // Log error here since request failed
            Log.e(LOG_TAG, t.toString());
        }
    });

Any ideas?

Thanks,

Theo

Theo
  • 3,099
  • 12
  • 53
  • 94

1 Answers1

2

You need to call ButterKnife.bind() inside of a method, you can't call it outside the method. And for fragment, you need to call it inside onCreateView():

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.fancy_fragment, container, false);
  ButterKnife.bind(this, view);
  // TODO Use fields...
  return view;
}

Please read the documentation at http://jakewharton.github.io/butterknife/

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96