Custom list View adapter have null NullPointerException error

I program in application to show word, but I have error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

I know my error refer to CustomBaseAadpter in the:

holder.txtWord.setText(rowItem.getword())

but I don't know how to fix.

CustomBaseAdapter.java

public class CustomBaseAdapter extends BaseAdapter {

    Context context;
    private List<item_row_adapter> rowItems;

    public CustomBaseAdapter(Context context, List<item_row_adapter> items) {
        this.context = context;
        this.rowItems = items;
    }

    private class ViewHolder {
        TextView txtWord;
        TextView txtMean;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_view, null);
            holder = new ViewHolder();
            holder.txtWord = (TextView) convertView.findViewById(R.id.word_word);
            holder.txtMean = (TextView) convertView.findViewById(R.id.mean_mean);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        item_row_adapter rowItem = (item_row_adapter) getItem(position);

        holder.txtWord.setText(rowItem.getword());
        holder.txtMean.setText(rowItem.getmean());

        return convertView;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return rowItems.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return rowItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return rowItems.indexOf(getItem(position));
    }
}

item_row_adapter.java

public class item_row_adapter {

    private String word ="";
    private String mean = "";

    public item_row_adapter(String word, String mean) {
        this.word = word;
        this.mean = mean;
    }

    public String getmean() {
        return mean;
    }

    public void setmean(String mean) {
        this.mean = mean;
    }

    public String getword() {
        return word;
    }

    public void setword(String word) {
        this.word = word;
    }

    public String tostring() {
        return mean + "\n" + word;
    }
}

vocab_1_program.java

public class vocab_1_program extends Activity implements OnItemClickListener {

    private ListView listview ;

    public static final String[] Word_item = new String[] { "Strawberry", "Banana", "Orange", "Mixed" };

    public static final String[] Mean_word = new String[] {
            "It is an aggregate accessory fruit",
            "It is the largest herbaceous flowering plant", "Citrus Fruit", "Mixed Fruits" };

    List<item_row_adapter> rowItems;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vocab_1_layout);

        rowItems = new ArrayList<item_row_adapter>();
        for (int i = 0; i < Word_item.length; i++) {
            item_row_adapter item = new item_row_adapter(Word_item[i],
                    Mean_word[i]);
            rowItems.add(item);
        }

        listview = (ListView) findViewById(R.id.listview_view);
        CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
        listview.setAdapter(adapter);
        listView.setOnItemClickListener((android.widget.AdapterView.OnItemClickListener) this);
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast toast = Toast.makeText(getApplicationContext(), "Item "
                + (position + 1) + ": " + rowItems.get(position),
                Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.show();
    }
}

word_row.xml

<TextView
    android:id="@+id/word_word"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="Header"
    android:textColor="@android:color/black" />

<TextView
    android:id="@+id/mean_mean"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="Mean" />

vocab_1_layout.xml

<ListView
    android:id="@+id/listview_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>

Answers


You are inflating wrong layout for the row.

R.layout.list_view

is not having the required view. You are having it in

word_row.xml

in your CustomBaseAadpter under the public View getView(int position, View convertView, ViewGroup parent) you are inflating the a xml which is not having the required view elements.

The method findViewById() returns a view instance corresponding to the id and present in the inflated layout. Since you have inflated the wrong layout and that layout is not having the view being found thats why the medthod findViewById() returns null for the view.

Note this line-

    holder.txtWord = (TextView) convertView.findViewById(R.id.word_word);
    holder.txtMean = (TextView) convertView.findViewById(R.id.mean_mean);

hence here holder.txtWord= null and holder.txtMean=null and when you set you call the methods setText() on the instance holder.txtWord it throws the NPE.


You got the error because you have inflated the wrong layout into your CustomBaseAdapter.

you have inflated list_view.xml instead of word_row.xml

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) 
    {

        LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.word_row, null);
        holder = new ViewHolder();
        holder.txtWord = (TextView) convertView.findViewById(R.id.word_word);
        holder.txtMean = (TextView) convertView.findViewById(R.id.mean_mean);
        convertView.setTag(holder);
    } else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    item_row_adapter rowItem = (item_row_adapter) getItem(position);

    holder.txtWord.setText(rowItem.getword());
    holder.txtMean.setText(rowItem.getmean());

    return convertView;
}

Need Your Help

Column names for a table formed by a UNION

sql sqlite union

Given a couple of simple tables like so:

Using git with emacs

git macos version-control emacs

I am trying to configure git.el. When i do git-status i can see the status buffer with changed i can also add files using 'a' but when i try to commit a file using c