Hay Abados
Hay Abados

Reputation: 53

NullException when I showing a number on TextView

i getting Null when i want to present data on a list view. the log tell that the problem is with this line:

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

end that's the error :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                        at com.example.hay.stormy.adapters.DayAdapter.getView(DayAdapter.java:64)

i also tried to just give a constant number or a constant String and its still crash with the same error, but if i run the app without this line i get al rest of the data that i need, weird...

some code for u to understand:

my ViewHolder and getView:

   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()+ "");

    if (position == 0) {
        holder.dayLabel.setText("Today");
    }
    else {
        holder.dayLabel.setText(day.getDayOfTheWeek());
    }

    return convertView;
}

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

and my getter and setter for the problematic parameter in the Day class:

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

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

Upvotes: 0

Views: 51

Answers (1)

Bene
Bene

Reputation: 734

It seems like temperatureLabel is null. You probably have set a wrong ID. findViewById returns null if the view can't be found. http://developer.android.com/reference/android/view/View.html#findViewById(int)

Upvotes: 3

Related Questions