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 trial

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

Runkun Miao
Runkun Miao
4,186 Points

NullPointerException returns when I click daily button

FATAL EXCEPTION: main Process: com.example.miaor.tutorialweather, PID: 4261 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference at com.example.miaor.tutorialweather.ForecastData.Forecast.getIconID(Forecast.java:44) at com.example.miaor.tutorialweather.ForecastData.Day.getIconID(Day.java:83) at com.example.miaor.tutorialweather.adapters.Adapter.getView(Adapter.java:63)

As my error log shows, the problem is from getting IconID and the debug report also reveals that the mIcon = null when the adapter is setting.

 public class Forecast {

 private Current mCurrent;
    private Hour[] mHours;
    private Day[] mDays;

     public static int getIconID(String weather){
        switch (weather){
            case "clear-day":
                return R.drawable.clear_day;
            case "clear-night":
                return R.drawable.clear_night;
            case "rain":
                return R.drawable.rain;
            case "snow":
                return R.drawable.snow;
            case "sleet":
                return R.drawable.sleet;
            case "wind":
                return R.drawable.wind;
            case "fog":
                return R.drawable.fog;
            case "cloudy":
                return R.drawable.cloudy;
            case "partly-cloudy-day":
                return R.drawable.partly_cloudy;
            case "partly-cloudy-nigh":
                return R.drawable.cloudy_night;
            default:
                return R.drawable.sunny;
        }
    }

}````

public class Day implements Parcelable{

private long mTime;
private String mSummary;
private double mTemperatureMax;
private String mIcon;
private String mTimeZone;

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

}

public class Adapter extends BaseAdapter {
    private Context mContext;
    private Day[] mDays;



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


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

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

    @Override
    public long getItemId(int position) {
        return 0; ///used to tag items for easy items
    }

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

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

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

        Day day = mDays[position];
        holder.iconImageView.setImageResource(day.getIconID());
        holder.temperatureLabel.setText(day.getTemperatureMax()+"");
        holder.dayLabel.setText(day.getDayOfTheWeek());
        return convertView;
    }


    private static class ViewHolder{
        ImageView iconImageView;
        TextView temperatureLabel;
        TextView dayLabel;
    }
}
Simon Coates
Simon Coates
28,694 Points

um, well i can't see anything that would set mIcon, and i'm not sure null is supported in a switch (see http://stackoverflow.com/questions/18263489/why-doesnt-string-switch-statement-support-a-null-case)