0

I create a adapter :

public class Adapter extends BaseAdapter implements Filterable {

    Context context;
    ArrayList<RowBean> data = null;
    private LayoutInflater inflater;
    private ValueFilter valueFilter;
    private ArrayList<RowBean> mStringFilterList;

    public Adapter(Context context, ArrayList<RowBean> data) {
        super();
        this.context = context;
        this.data = data;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mStringFilterList = data;
        getFilter();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RowBeanHolder holder;
        RowBean rowBean = data.get(position);
        if(convertView == null){
            holder = new RowBeanHolder();
            convertView = inflater.inflate(R.layout.all_object_holder,null);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.showText);
            holder.selected = (CheckBox) convertView.findViewById(R.id.checkBox12);
        }else{
            holder = (RowBeanHolder) convertView.getTag();
        }
        if(rowBean != null) {
            holder.txtTitle.setText(rowBean.getTitle());
            holder.selected.setChecked(rowBean.isSelected());
        }

        return convertView;
    }

    @Override
    public Filter getFilter() {
        if(valueFilter==null) {

            valueFilter=new ValueFilter();
        }

        return valueFilter;
    }

    private  class RowBeanHolder {
        TextView txtTitle;
        CheckBox selected;
    }

    private class ValueFilter extends Filter {

        //Invoked in a worker thread to filter the data according to the constraint.
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results=new FilterResults();
            if(constraint!=null && constraint.length()>0){
                ArrayList<RowBean> filterList=new ArrayList<>();
                for(int i=0;i<mStringFilterList.size();i++){
                    if((mStringFilterList.get(i).getTitle().toUpperCase())
                            .contains(constraint.toString().toUpperCase())) {
                        RowBean contacts = new RowBean();
                        contacts.setTitle(mStringFilterList.get(i).getTitle());
                        contacts.setSelected(mStringFilterList.get(i).isSelected());
                        filterList.add(contacts);
                    }
                }
                results.count=filterList.size();
                results.values=filterList;
            }else{
                results.count=mStringFilterList.size();
                results.values=mStringFilterList;
            }
            return results;
        }


        //Invoked in the UI thread to publish the filtering results in the user interface.
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            data=(ArrayList<RowBean>) results.values;
            notifyDataSetChanged();
        }
    }
}

When I start my activity with this adapter I log I see this : Process: com.maps, PID: 20174 java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.maps.Adapter.Adapter$RowBeanHolder.txtTitle' on a null object reference

this is a line :

holder.txtTitle.setText(rowBean.getTitle());

This is a layout all_object_holder:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <CheckBox
        android:id="@+id/checkBox12"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:clickable="false"
        android:button="@xml/setting_checkbox"
        android:focusable="false"
        android:gravity="center" />

    <TextView
        android:id="@+id/showText"
        android:layout_width="395dp"
        android:layout_height="wrap_content"
        android:text="@string/show_choose_point"
        android:textColor="#000000"
        android:layout_alignBaseline="@+id/checkBox12"
        android:layout_alignBottom="@+id/checkBox12"
        android:layout_toRightOf="@+id/checkBox12"
        android:layout_toEndOf="@+id/checkBox12" />


</RelativeLayout>
  • you have forgot to set Tag in convertView in if condition. – Chetan Joshi Mar 07 '17 at 13:55
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – John Joe Mar 09 '17 at 08:37

4 Answers4

2

Add line

convertView.setTag(holder);

without it convertView.getTag() will return null.

Okas
  • 2,664
  • 1
  • 19
  • 26
1
 if(convertView == null){
             holder = new RowBeanHolder();
             convertView = inflater.inflate(R.layout.all_object_holder,null);
             holder.txtTitle = (TextView) convertView.findViewById(R.id.showText);
             holder.selected = (CheckBox) 
             convertView.setTag(holder);
}

You didn't add convertView.setTag(holder). Please add it to your code and check.

Harsh Soni
  • 52
  • 7
0

NullPointerException occurs because your holder object is null sometimes. You should setTag of your convertView, just add convertView.setTag(holder); method to getView method like that:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    RowBeanHolder holder;
    RowBean rowBean = data.get(position);
    if(convertView == null){
        holder = new RowBeanHolder();
        convertView = inflater.inflate(R.layout.all_object_holder,null);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.showText);
        holder.selected = (CheckBox) convertView.findViewById(R.id.checkBox12);
        convertView.setTag(holder);
    }else{
        holder = (RowBeanHolder) convertView.getTag();
    }
    if(rowBean != null) {
        holder.txtTitle.setText(rowBean.getTitle());
        holder.selected.setChecked(rowBean.isSelected());
    }

    return convertView;
}

Good luck.

Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
-3

Update your code with this code

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    RowBeanHolder holder = new RowBeanHolder();
    RowBean rowBean = data.get(position);
    if(convertView == null){
        convertView = inflater.inflate(R.layout.all_object_holder,null);
    }
    holder.txtTitle = (TextView) convertView.findViewById(R.id.showText);
    holder.selected = (CheckBox) convertView.findViewById(R.id.checkBox12);
    if(rowBean != null) {
        holder.txtTitle.setText(rowBean.getTitle());
        holder.selected.setChecked(rowBean.isSelected());
    }

    return convertView;
}
Rahul Giradkar
  • 1,818
  • 1
  • 17
  • 28
  • Whats the point of the View Holder if you are calling findViewById() every time ? – rckrd Mar 07 '17 at 13:58
  • holder is created for every time when get view is called. So we need to assign views to holder. other wise it will thought null point exception. – Rahul Giradkar Mar 08 '17 at 04:44