简体   繁体   中英

Problems with getFilter(): java.lang.NullPointerException

I have a problem with my getFilter(), when I populate the RecyclerView on Activity everything is working fine, but when I start typing on SearchView this error occurs:

java.lang.NullPointerException: Attempt to invoke virtual method 'package.Classes.Farm.getFarms()' on a null object reference

Why when I populate my RecyclerView is not null (Farm.class) and before, when I starting to type for search, is null?

In Activity I start my setOnQueryTextListener after the Callback response and the RecyclerView populated. I use this same code for months and never had a problem, now they changed de response callback and this errors occurs (Before (response example): https://jsonplaceholder.typicode.com/posts , and the getFilter() works like a charm).

(If you need more code and details tell me and I edit my question. My english is rusty)

Response OkHttp:

{
  "farms": [
    {
      "id": "000000000-0000-0000-0000-000000000000",
      "name": "Areado"
    }
  ]
}

The 1st class generated:

public class Farm {

@SerializedName("farms")
@Expose
private List<Farm_> farms = null;

public List<Farm_> getFarms() {
return farms;
}

public void setFarms(List<Farm_> farms) {
this.farms = farms;
}

}

The 2nd class generated:

public class Farm_ {

@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

The getFilter() method from adapter ( farmListFiltred and farmList is from type Farm.class ):

@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            String charString = charSequence.toString();
            if (charString.isEmpty()) {
                farmListFiltred = farmList;
            } else {
                Farm filteredList;
                filteredList = new Farm();
                for (Farm_ row : farmList.getFarms()) {
                    if (row.getName().toLowerCase().contains(charString.toLowerCase())) {
                        filteredList.getFarms().add(row);
                    }
                }

                farmListFiltred = filteredList;
            }

            FilterResults filterResults = new FilterResults();
            filterResults.values = farmListFiltred;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            farmListFiltred = (Farm) filterResults.values;
            notifyDataSetChanged();
        }
    };
}

well in your Farm class the farms member variable is set to null initially. It only gets a value when the setter of Farm class is called, not via a constructor. So calling filteredList.getFarms(), is expected to produce a nullPointer exception. I would suggest just doing something like

private List<Farm_> farms = new List<Farm_>();

in your Farm class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM