Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Lists and Adapters (2015) Using Parcelable Data Retrieving Parcelable Data

Guy Bridge
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Guy Bridge
Android Development Techdegree Graduate 24,991 Points

Getting a NullPointerException when I click the 7 Day button

Hi,

I've seen a few other similar questions but I still can't seem to get it working. When I click the 7 DAY button the app crashes;

java.lang.NullPointerException: Attempt to invoke virtual method 'int au.com.wsit.stormy.weather.Day.getTemperatureMax()' on a null object reference at au.com.wsit.stormy.adapters.DayAdapter.getView(DayAdapter.java:71)

My line 71 of DailyAdapter.java is; holder.temperatureLabel.setText(day.getTemperatureMax() + ""); However even if I swap that with the other holders;

    holder.dayLabel.setText(day.getDayOfTheWeek());
    holder.iconImageView.setImageResource(day.getIconId());

It will still crash on the first one. If I debug to line 71, then I see the day object has variables in it.

See below for the DayAdapter.java

public class DayAdapter extends BaseAdapter {

private Context mContext;
private Day[] mDays;

public DayAdapter(Context context, Day[] days)
{
    mContext = context;
    mDays = days;
}

@Override
public int getCount() {
    return mDays.length;
}

@Override
public Object getItem(int position) {
    return mDays[position];
}

@Override
public long getItemId(int position) {
    return 0; // We aren't going to use this. Tag items for easy reference
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;



    if (convertView == null)
    {
        // Brand new
        convertView = LayoutInflater.from(mContext).inflate(R.layout.daily_list_item, null);
        holder = new ViewHolder();
        holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
        holder.temperatureLabel = (TextView) convertView.findViewById(R.id.temperatureLabel);
        holder.dayLabel = (TextView) convertView.findViewById(R.id.dayNameLabel);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    Day day = mDays[position];


    holder.temperatureLabel.setText(day.getTemperatureMax() + "");
    holder.dayLabel.setText(day.getDayOfTheWeek());
    holder.iconImageView.setImageResource(day.getIconId());



    return convertView;
}

private static class ViewHolder
{
    ImageView iconImageView;
    TextView temperatureLabel;
    TextView dayLabel;
}

}

My Day.java class;

public class Day implements Parcelable { private long mTime; private String mSummary; private double mTemperatureMax; private String mIcon; private String mTimezone;

public long getTime() {
    return mTime;
}

public void setTime(long time) {
    mTime = time;
}

public String getSummary() {
    return mSummary;
}

public void setSummary(String summary) {
    mSummary = summary;
}

public int getTemperatureMax()
{
    return (int) Math.round(mTemperatureMax);

}

public void setTemperatureMax(double temperatureMax) {
    mTemperatureMax = temperatureMax;
}

public String getIcon() {
    return mIcon;
}

public void setIcon(String icon) {
    mIcon = icon;
}

public String getTimezone() {
    return mTimezone;
}

public void setTimezone(String timezone) {
    mTimezone = timezone;
}

public int getIconId()
{
    return Forecast.getIconId(mIcon);
}

public String getDayOfTheWeek()
{
    SimpleDateFormat formatter = new SimpleDateFormat("EEEE");
    formatter.setTimeZone(TimeZone.getTimeZone(mTimezone));

    Date dateTime = new Date(mTime * 1000);

    return formatter.format(dateTime);
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags)
{
    dest.writeLong(mTime);
    dest.writeString(mSummary);
    dest.writeDouble(mTemperatureMax);
    dest.writeString(mIcon);
    dest.writeString(mTimezone);
}

private Day(Parcel in)
{
    mTime = in.readLong();
    mSummary = in.readString();
    mTemperatureMax = in.readDouble();
    mIcon = in.readString();
    mTimezone = in.readString();

}

public Day() {}

public static final Creator<Day> CREATOR = new Creator<Day>() {
    @Override
    public Day createFromParcel(Parcel source) {
        return new Day(source);
    }

    @Override
    public Day[] newArray(int size) {
        return new Day[size];
    }
};

Hello,

I'm not seeing anything immediately popping out as incorrect in your code. Could you also post the 7 day activity? Or even better, post your full source somewhere like GitHub? That would help us in being able to you debug your code.

Hello,

I think you uploaded the wrong project. I'm seeing Ribbit instead of Stormy.

I'm still investigating, but it looks llike your JSONData is getting cut short so that it only actually fills 3 of the 8 days in the array and so when you cycle through them, you hit the 4th Day and find that it is null. I'm not sure right at the moment why its getting cut short, but I'm still looking into it.

Edit I was wrong on the data being cut short, my display just truncated things, but see my answer below for what looks to be the actual problem.

2 Answers

Hello,

I found the error in your code. In getDailyForecast in MainActivity, your for loop goes from 0 to daily.length(), when you need it to be going from 0 to data.length(). If you change that, you should be able to get the 7 days to not crash. If you need a better explaination or more help, please let us know and we'll try to assist you further.