0

here code is Of the class

public class AdapterOrderShop extends 
RecyclerView.Adapter<AdapterOrderShop.HolderOrderShop>
implements Filterable {

private Context context;
public ArrayList<ModelOrderShop> orderShopArrayList,filterList;
private FilterOrderShop filter;

 public AdapterOrderShop(Context context, ArrayList<ModelOrderShop> orderShopArrayList) {
    this.context = context;
    this.orderShopArrayList = orderShopArrayList;

    this.filterList=orderShopArrayList;
}

@Override
public int getItemCount() {
     return orderShopArrayList.size();
} 

Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference

  • As the error says, You didn't set the list and it is null. Are You passing a list in the constructor to the Adapter? You have to init `orderShopArrayList` in some way. Maybe show full adapter class and a place where You create it and someone will help You, now I think it is impossible – iknow Nov 12 '20 at 10:35

1 Answers1

0

You should initialize your list in the adapter constructor and the problem is solved. Uninitialized list return the null that why this error occur. You can create the constructor in adapter and then the list passing from the activity or fragment assign to the list declare in the adapter. If you are passing the context from the activity or fragment then you have the same constructor just like below but if you are not passing the context then remove the Context parameter from the constructor.

public YourAdapterName(Context mCtx, List<YourModelClassName> taskList) {
    this.mCtx = mCtx;
    this.orderShopArrayList= taskList;
}
Syed Rafaqat Hussain
  • 1,009
  • 1
  • 9
  • 33