SexualPotatoes SexualPotatoes - 4 months ago 40
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 android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$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;
}

@Override
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;
}

@Override
public void onBindViewHolder(HourViewHolder holder, int position) {
holder.bindHour(mHours[position]);
}

@Override
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) {
super(itemView);
mTimeLabel = (TextView) itemView.findViewById(R.id.timeLabel);
mTemperatureLabel = (TextView) itemView.findViewById(R.id.temperatureLabel);
mSummaryLabel = (TextView) itemView.findViewById(R.id.summaryLabel);
mIconImageView = (ImageView) itemView.findViewById(R.id.iconImageView);
}

public void bindHour(Hour hour) {
mTimeLabel.setText(hour.getHour());
mSummaryLabel.setText(hour.getSummary());
mTemperatureLabel.setText((int)hour.getTemperature());
mIconImageView.setImageResource(hour.getIconId());
}

}
}


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(R.id.recyclerView) RecyclerView mRecyclerView;

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

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

HourAdapter adapter = new HourAdapter(mHours);
mRecyclerView.setAdapter(adapter);

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);

}
}


What am I overlooking?

Thanks in advance for the wisdom!

Answer

Try calling:

ButterKnife.bind(this);

after setting the content view

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