android – SearchView adapter – ‘java.lang.Object[] java.util.Collection.toArray()’ on a null object reference

I have trouble searching my data from the remote server to my RecyclerView. It is showing the above-mentioned error, and the app crashes as soon as I Search from the list. I have highlighted the functions where the error occurs in log cat. I also mentioned where is the function in my classes.

Adapter Classes:-

public class Question_Adapter extends RecyclerView.Adapter<Question_Adapter.QuestionViewHolder> implements Filterable {

private List<Question_Model> question_modelList = new ArrayList<Question_Model>();
private List<Question_Model> question_models_another_list;
Context context;
public Question_Adapter(List<Question_Model> question_modelList){
    this.question_modelList = question_modelList;
    question_models_another_list = new ArrayList<>(question_modelList);
}

class QuestionViewHolder extends RecyclerView.ViewHolder {
    TextView semester, course_name, course_teacher, session_year;
    ImageView readPDF, downPDF;
    CardView card;
    public QuestionViewHolder(@NonNull View itemView) {
        super(itemView);
        semester = itemView.findViewById(R.id.semester);
        course_name = itemView.findViewById(R.id.course_name);
        course_teacher = itemView.findViewById(R.id.course_teacher);
        session_year = itemView.findViewById(R.id.session_year);
        card = itemView.findViewById(R.id.card);
        readPDF = itemView.findViewById(R.id.readPDF);
        downPDF = itemView.findViewById(R.id.downPDF);
    }
}
@NonNull
@Override
public Question_Adapter.QuestionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pdf_view_design,parent,false);
    context = parent.getContext();
    return new QuestionViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull Question_Adapter.QuestionViewHolder holder, int position) {
    Question_Model current_book = question_modelList.get(position);
    holder.semester.setText(current_book.getSemester());
    holder.course_name.setText(current_book.getCourse_name());
    holder.course_teacher.setText(current_book.getCourse_teacher());
    holder.session_year.setText(current_book.getExam_type()+" - "+current_book.getSession_name());

    holder.readPDF.setOnClickListener(v -> readPDFInformation(current_book.getPdf_link()));
    String file_name = current_book.getYear()+"_"+current_book.getSemester()+"_"+current_book.getCourse_name()+"_"+current_book.getCourse_teacher();
    holder.downPDF.setOnClickListener(v -> downPDFInformation(current_book.getPdf_link(),file_name));
}

public void sortNameByAsc() {
    Comparator<Question_Model> comparator = new Comparator<Question_Model>() {

        @Override
        public int compare(Question_Model object1, Question_Model object2) {
            return object1.getSemester().compareToIgnoreCase(object2.getSemester());
        }
    };
    Collections.sort(question_modelList, comparator);
    notifyDataSetChanged();
}

public void sortFilterByAsc() {
    Comparator<Question_Model> comparator = new Comparator<Question_Model>() {
        @Override
        public int compare(Question_Model object1, Question_Model object2) {
            return object1.getSemester().compareToIgnoreCase(object2.getSemester());
        }
    };
    Collections.sort(question_models_another_list, comparator);
    notifyDataSetChanged();
}

private long downPDFInformation(String url, String fileName) {

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url + ""));
    request.setTitle(fileName);
    request.setMimeType("application/pdf");
    request.allowScanningByMediaScanner();
    request.setAllowedOverMetered(true);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "PUC/" + fileName+".pdf");
    DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
    return downloadManager.enqueue(request);
}

private void readPDFInformation(String url) {
    try {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        context.startActivity(intent);
    } catch (Exception e) {
        //error
    }
}

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

public Filter getFilter() {
    return filter;
}

public Filter filter= new Filter() {
    @Override
    public FilterResults performFiltering(CharSequence constraint) {
        List<Question_Model> filterlist=new ArrayList<>();
        if(constraint==null|| constraint.length()==0){
            filterlist.addAll(question_models_another_list);
        }
        else{
            String pattrn=constraint.toString().toLowerCase().trim();
            for(Question_Model item :question_models_another_list){
                if(item.getCourse_name().toLowerCase().contains(pattrn) ||
                        item.getCourse_teacher().toLowerCase().contains(pattrn)||
                        item.getYear().toLowerCase().contains(pattrn)||
                        item.getSemester().toLowerCase().contains(pattrn)||
                        item.getExam_type().toLowerCase().contains(pattrn)||
                        item.getPdf_file_name().toLowerCase().contains(pattrn)){
                    filterlist.add(item);
                }
            }
        }
        FilterResults filterResults=new FilterResults();
        filterResults.values=filterlist;
        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        question_modelList.clear();
        question_modelList.addAll((List)results.values);
        notifyDataSetChanged();
    }
};
}

Model Class:-

public class Question_Model {
private String exam_type, semester, year, session_name, course_teacher, course_name, pdf_link, pdf_file_name, upload_time, key;

public Question_Model(String exam_type, String semester, String year, String session_name, String course_teacher, String course_name, String pdf_link, String upload_time, String pdf_file_name, String key) {
    this.exam_type = exam_type;
    this.semester = semester;
    this.year = year;
    this.session_name = session_name;
    this.course_teacher = course_teacher;
    this.course_name = course_name;
    this.pdf_link = pdf_link;
    this.upload_time = upload_time;
    this.pdf_file_name = pdf_file_name;
    this.key = key;
}

public String getExam_type() {
    return exam_type;
}

public void setExam_type(String exam_type) {
    this.exam_type = exam_type;
}

public String getSession_name() {
    return session_name;
}

public void setSession_name(String session_name) {
    this.session_name = session_name;
}

public String getPdf_file_name() {
    return pdf_file_name;
}

public void setPdf_file_name(String pdf_file_name) {
    this.pdf_file_name = pdf_file_name;
}

public String getSemester() {
    return semester;
}

public void setSemester(String semester) {
    this.semester = semester;
}

public String getYear() {
    return year;
}

public void setYear(String year) {
    this.year = year;
}

public String getCourse_teacher() {
    return course_teacher;
}

public void setCourse_teacher(String course_teacher) {
    this.course_teacher = course_teacher;
}

public String getCourse_name() {
    return course_name;
}

public void setCourse_name(String course_name) {
    this.course_name = course_name;
}

public String getPdf_link() {
    return pdf_link;
}

public void setPdf_link(String pdf_link) {
    this.pdf_link = pdf_link;
}

public String getUpload_time() {
    return upload_time;
}

public void setUpload_time(String upload_time) {
    this.upload_time = upload_time;
}

public String getKey() {
    return key;
}

public void setKey(String key) {
    this.key = key;
}
}

Logcat

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tanzim.pucquestionbot, PID: 16358
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
    at java.util.ArrayList.addAll(ArrayList.java:588)
    at com.tanzim.pucquestionbot.home.Question_Adapter$3.publishResults(Question_Adapter.java:176)
    at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:284)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

***NB: Searching performing from androidx.appcompat.widget.SearchView ***

Leave a Comment