1. Are you ready for the Galaxy S20? Here is everything we know so far!

Filter My Recyclerview

Discussion in 'Android Development' started by GotParty, May 30, 2020.

  1. GotParty

    GotParty Lurker
    Thread Starter

    I'm trying to implement a search view for my hockey app this is the error I get.

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference

    Here is my code

    public class HockeyAdapter extends RecyclerView.Adapter<HockeyAdapter.ViewHolder> implements Filterable {

    private ArrayList<Player> playerList;
    private List<Player> playerFull;
    RequestOptions option;
    private Context context;

    public HockeyAdapter(ArrayList<Player> playerList, Context context) {
    this.playerList = playerList;
    this.context = context;
    playerFull = new ArrayList<>(playerList);

    //request glide
    option = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);


    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    view = layoutInflater.inflate(R.layout.hockey_cell, parent, false);
    return new ViewHolder(view);

    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    final Player player = playerList.get(position);

    //Load image with glide

    //Load text


    public int getItemCount() {
    return playerList.size();

    public Filter getFilter() {
    return exampleFilter;

    private Filter exampleFilter = new Filter() {
    protected FilterResults performFiltering(CharSequence constraint) {
    ArrayList<Player> filteredList = new ArrayList<>();
    if (constraint == null || constraint.length() == 0) {
    } else {
    String filteredPattern = constraint.toString().toLowerCase().trim();
    for (Player item : playerFull) {
    if (item.getName().toLowerCase().contains(filteredPattern)) {
    FilterResults results = new FilterResults();
    results.values = filteredList;
    return results;

    protected void publishResults(CharSequence constraint, FilterResults results) {
    if (results.values != null) {
    playerList.addAll((Collection<? extends Player>) results.values);

    public class ViewHolder extends RecyclerView.ViewHolder {

    public final ImageView pImg;
    public final TextView name, position, team;

    public ViewHolder(@NonNull View view) {
    this.name = view.findViewById(R.id.name);
    this.position = view.findViewById(R.id.position);
    this.pImg = view.findViewById(R.id.imageView);
    this.team = view.findViewById(R.id.team);


    1. Download the Forums for Android™ app!


  2. marcelpreda

    marcelpreda Member

    And the question is?
  3. GotParty

    GotParty Lurker
    Thread Starter

    How do I get my search view to work?
  4. wseng92

    wseng92 Android Enthusiast

    Perhaps playerFull is null?
  5. My suggestion is to make sure that item.getName() is actually returning a non-null string, or to check that it is non-null. It appears that the variable returned by getName() is not getting initialized by the Player Class.

Share This Page