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

Yet another null pointer exception - DayAdapter ConvertView is null

Like a lot of ppl i'm getting a null pointer exception when I click DAILY. It errors on line 49 of Day adapter which is

convertView = LayoutInflater.from(mContext).inflate(R.layout.daily_list_item, null);

which is the same as in the package files. i ran the debugger and all i found out was what I already knew, that the convertView was coming up null. here's my DayAdapter, let me know if theres any other info that could be valuable.

package com.example.paxie.stormy.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.paxie.stormy.R;
import com.example.paxie.stormy.weather.Day;

/**
 * Created by paxie on 9/17/15.
 */
public class DayAdapter extends BaseAdapter {

private Context mContext;
private Day[] mDays;

public DayAdapter(Context context, Day[] days) {

    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.iconImageView.setImageResource(day.getIconId());

    holder.temperatureLabel.setText(day.getTemperatureMax() + "");

    holder.dayLabel.setText(day.getDayOfTheWeek());

    return null;
}

private static class ViewHolder {
    ImageView iconImageView; //public by default
    TextView temperatureLabel;
    TextView dayLabel;

}



}

4 Answers

Seth Kroger
Seth Kroger
56,407 Points

You're forgetting to set mContext in the constructor, which will cause LayoutInflater.from(mContext) to throw a NullPointerExecption.

i have

private Context mContext;

at the top. is there someone else it needs referenced?

Seth Kroger
Seth Kroger
56,407 Points
public DayAdapter(Context context, Day[] days) {
    mContext = context; // <--needs to be set here.
    mDays = days;

}

i see now. i made that change and am getting a new error though

10-14 15:48:22.018 1040-1040/com.example.paxie.stormy E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference

ok nvm figured that one out. had to change return null to return convertView. Thank you!