September
September

Reputation: 45

NullPointerException while working with convertView and custom data listView

I am working with custom data listViews and a convertView to create a basic listView which can be populated with custom data, I keepe getting a null Pointer exception at a block of code near the bottom which has "holder.text1.setText(X);" etc. I can't figure out why and how to solve the problem.

(As recommended I initialized the text views in the else statement and it still threw the same exception)

 package com.example.worksheet2;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

 public class D extends ListActivity {

final class DataMap {
    public String contactName = "";
    public int followerCount;


    public DataMap(String contactName, int followercount) {
        this.contactName = contactName;
        this.followerCount = followercount;
    }

}

protected LayoutInflater mLayoutInflater;
protected Context mContext = this;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.home_list_view);
    mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    DataMap[] data = new DataMap[] {

    new DataMap("Person A", 1),
    new DataMap("Person B", 6),
    new DataMap("Person C", 6),
    new DataMap("Person D", 6),
    new DataMap("Person E", 6)

    };

    setListAdapter(new listViewAdapter(this, R.layout.home_list_view_list_item, data));

}

class ViewHolder {
    public ImageView avatar;
    public TextView text1, text2;

}

final class listViewAdapter extends ArrayAdapter<DataMap> {
    private int layoutResource;
    public DataMap[] dataObjects;

    public listViewAdapter(Context context, int resource, DataMap[] objects) {
        super(context, resource, objects);
        layoutResource = resource;
        dataObjects = objects;
    }

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

        TextView monkey = (TextView) findViewById(R.id.monkey);
        ViewHolder holder;
        if (convertView == null) {
        convertView=                                                 mLayoutInflater.inflate(layoutResource,       null, false);
            holder = (ViewHolder) convertView.getTag();
            holder = new ViewHolder();
            holder.avatar = (ImageView) findViewById(R.id.avatar);
            holder.text1 = (TextView) findViewById(R.id.title);
            holder.text2 = (TextView) findViewById(R.id.link);

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


        }


        DataMap data = dataObjects[position];
        holder.text1.setText(data.contactName);
        holder.text2.setText(data.followerCount);
        holder.avatar.setImageResource(R.drawable.ic_launcher);

        return convertView;
    }

}

}

EDIT

Here is the log/stack trace that was asked for:

   02-15 17:45:53.181: E/ListView(1006): null
   02-15 17:45:53.181: E/ListView(1006): java.lang.NullPointerException
   02-1517:45:53.181:E/ListView(1006):at com.example.worksheet2.D$listViewAdapter.getView(D.java:89)                                                   
02-15 17:45:53.181: E/ListView(1006):   at android.widget.AbsListView.obtainView(AbsListView.java:1449)
02-15 17:45:53.181: E/ListView(1006):   at android.widget.ListView.makeAndAddView(ListView.java:1801)
02-15 17:45:53.181: E/ListView(1006):   at android.widget.ListView.fillDown(ListView.java:671)
02-15 17:45:53.181: E/ListView(1006):   at android.widget.ListView.fillFromTop(ListView.java:728)

Upvotes: 1

Views: 1086

Answers (3)

Pramod
Pramod

Reputation: 1

Change your getView() method as follows:

TextView monkey = (TextView) findViewById(R.id.monkey);
        ViewHolder holder;
        if (convertView == null) {
        convertView = mLayoutInflater.inflate(layoutResource, null, false);
            holder = (ViewHolder) convertView.getTag();
            holder = new ViewHolder();
            holder.avatar = (ImageView) findViewById(R.id.avatar);
            holder.text1 = (TextView) findViewById(R.id.title);
            holder.text2 = (TextView) findViewById(R.id.link);

            convertView.setTag(holder);

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

        DataMap data = dataObjects[position];
        holder.text1.setText(data.contactName);
        holder.text2.setText(data.followerCount);
        holder.avatar.setImageResource(R.drawable.ic_launcher);

Upvotes: 0

Danation
Danation

Reputation: 793

You're not setting holder.text1, holder.text2, or holder.avatar in your else block of listViewAdapter.getView(). I suggest a change similar to this:

    ...
    if (convertView == null) {
        convertView = mLayoutInflater.inflate(layoutResource,       null, false);
        holder = (ViewHolder) convertView.getTag();
        holder = new ViewHolder();

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
        holder = new ViewHolder();
    }
    holder.avatar = (ImageView) findViewById(R.id.avatar);
    holder.text1 = (TextView) findViewById(R.id.title);
    holder.text2 = (TextView) findViewById(R.id.link);
    ...

Upvotes: 0

panoptical
panoptical

Reputation: 812

Looking only at the getView method:

It looks like holder.text1 and holder.text2 could be null if convertView is not null, as nothing actually initializes text1 and text2 as TextViews outside the if statement. I would fix that in the else code block.

Next time, a stack trace of the exception would be nice to see exactly what line the exception is occurring on.

Upvotes: 4

Related Questions