SexualPotatoes SexualPotatoes - 1 year ago 155
Java Question

RecyclerView Null Pointer Exception

I'm new to Android Studio, and have found similar questions but none with replies that apply to my current instance. I've been trying to work with RecyclerViews and have been getting the following error message whenever I open an activity with it. The code doesn't show any errors.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lucasrizzotto.edgeweather/com.lucasrizzotto.edgeweather.ui.HourlyForecastActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void$LayoutManager)' on a null object reference

This is my adapter:

public class HourAdapter extends RecyclerView.Adapter<HourAdapter.HourViewHolder> {

private Hour[] mHours;

public HourAdapter(Hour[] hours) {
mHours = hours;

public HourViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).
inflate(R.layout.hourly_list_item, parent, false);
HourViewHolder viewHolder = new HourViewHolder(view);
return viewHolder;

public void onBindViewHolder(HourViewHolder holder, int position) {

public int getItemCount() {
return mHours.length;

public class HourViewHolder extends RecyclerView.ViewHolder {

public TextView mTimeLabel;
public TextView mTemperatureLabel;
public TextView mSummaryLabel;
public ImageView mIconImageView;

public HourViewHolder(View itemView) {
mTimeLabel = (TextView) itemView.findViewById(;
mTemperatureLabel = (TextView) itemView.findViewById(;
mSummaryLabel = (TextView) itemView.findViewById(;
mIconImageView = (ImageView) itemView.findViewById(;

public void bindHour(Hour hour) {


And my Activity class (Keep in mind I'm using the ButterKnife plugin to avoid boilerplate code)

public class HourlyForecastActivity extends AppCompatActivity {

private Hour[] mHours;

@BindView( RecyclerView mRecyclerView;

protected void onCreate(Bundle savedInstanceState) {

Intent intent = getIntent();
Parcelable[] parcelables = intent.getParcelableArrayExtra(MainActivity.HOURLY_FORECAST);
mHours = Arrays.copyOf(parcelables, parcelables.length, Hour[].class);

HourAdapter adapter = new HourAdapter(mHours);

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);


What am I overlooking?

Thanks in advance for the wisdom!

Answer Source

Try calling:


after setting the content view

Since you are not doing this the conventional way, you need to actually call bind().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download