Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPranay Sashank Pisupati
2,674 PointsNull Pointer Exception.. App crashes on clicking Hourly Button.
The App Stormy crashes on clicking the Hourly Button. This question is followed by the answer for this problem. The error lies in HourAdapter class where we set mIconImageView.
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,mSummarylabel,mTemperatureLabel;
public ImageView mIconImageView;
public HourViewHolder(View itemView) {
super(itemView);
mTimeLabel = (TextView) itemView.findViewById(R.id.timeHourLabel);
mTemperatureLabel = (TextView) itemView.findViewById(R.id.temperatureHourLabel);
mSummarylabel = (TextView) itemView.findViewById(R.id.summaryHourLabel);
mIconImageView = (ImageView) itemView.findViewById(R.id.iconHourImageView);
}
public void bindHour(Hour hour){
mTimeLabel.setText(hour.getHour());
mTemperatureLabel.setText(hour.getTemperature() + "");
mSummarylabel.setText(hour.getSummary());
mIconImageView.setImageResource(hour.getIconId());
}
}
}
2 Answers
Pranay Sashank Pisupati
2,674 PointsThe error is caused at mIconImageView.setImageResource which is throwing a Null pointer exception.
The error arises because mIcon is set to null. In the Main Activity, when calling getHourlyForecastActivity, you should set
hour.setIcon(jsonHour.getString("Hour");
which i think many forgot to do because of which mIcon is set to default null and setImageResource is trying to throw an NullPointerException. If you have called setIcon on hour it should work fine.
Pranay Sashank Pisupati
2,674 PointsSeth Kroger The error arises because mIcon is set to null. In the Main Activity, you should set
hour.setIcon(jsonHour.getString("Hour");
which i think many forgot to do because of which mIcon is set to default null and setImageResource is trying to throw an NullPointerException. If you have called setIcon on hour it should work fine. I'll update the answer soon.
Seth Kroger
56,413 PointsSeth Kroger
56,413 PointsThe problem with both solutions is that they are only covering over the problem instead of fixing it. Why is mIconImageView null and is that reason something you can fix?
TheBigoso /\
3,217 PointsTheBigoso /\
3,217 PointsYou forgot a close ) in it should look like this
hour.setIcon(jsonHour.getString("Hour"));
But when you run this your whole app crashes it should be
hour.setIcon(jsonHour.getString("icon"));