'Implementing search bar and getting 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference error

I am new to android studio and I have been trying to implement a search bar onto my app and I've come across a building error. It install's fine but when I try to open the app on my phone shows "application has stopped".

Logcat (Errors):

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sliteproj, PID: 1445
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sliteproj/com.example.sliteproj.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
    at java.util.ArrayList.<init>(ArrayList.java:191)
    at com.example.sliteproj.Adapter.<init>(Adapter.java:34)
    at com.example.sliteproj.MainActivity.showRecord(MainActivity.java:57)
    at com.example.sliteproj.MainActivity.onCreate(MainActivity.java:41)
    at android.app.Activity.performCreate(Activity.java:7183)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6944) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

I understand that this is a frequently asked question, and have looked through the other answers and solutions. I have tried initializing the List but still, am unable to fix the error.I think I am having trouble understanding where the initialization is exactly needed.

My code (Adapter.java):

public class Adapter extends RecyclerView.Adapter<Adapter.Holder> implements 
Filterable {
private Context context;
private ArrayList<Model> arrayList;
private ArrayList<Model> exampleArrayList;

DatabaseHelper databaseHelper;

public Adapter(Context context, ArrayList<Model> arrayList) {
    this.context = context;
    this.arrayList = arrayList;
    exampleArrayList = new ArrayList<>(exampleArrayList);

    databaseHelper = new DatabaseHelper(context);
}

@Override
public Filter getFilter() {
    return exampleFilter;
}

private Filter exampleFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        List<Model> filteredList = new ArrayList<>();

        if (constraint == null || constraint.length() == 0) {
            filteredList.addAll(exampleArrayList);
        } else {
            String filterPattern = constraint.toString().toLowerCase().trim();

            for (Model item: exampleArrayList) {
                if (item.getName().toLowerCase().contains(filterPattern)) {
                    filteredList.add(item);
                }
            }
        }

        FilterResults results = new FilterResults();
        results.values = filteredList;

        return results;
    }

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


Solution 1:[1]

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference

The first line declares a variable named exampleArrayList, but it does not actually contain a primitive value yet. Instead, it contains a pointer (because the type is ArrayList<Model> which is a reference type). Since you have not yet said what to point to, Java sets it to null, which means "I am pointing to nothing".

public Adapter(Context context, ArrayList<Model> arrayList) {
    this.context = context;
    this.arrayList = arrayList;
    //this line you are assing array list from null object thats why you are getting **NullPointerException**
    exampleArrayList = new ArrayList<>(exampleArrayList);

    databaseHelper = new DatabaseHelper(context);
}

Solution

replace exampleArrayList = new ArrayList<>(exampleArrayList); with exampleArrayList = new ArrayList<>() or if you want to assign array which is comming from constructor then exampleArrayList = new ArrayList<>(arrayList); or exampleArrayList = arrayList

Edit 2

private void showRecord() {

    adapter = new Adapter(MainActivity.this,
            databaseHelper.getAllData(Constants.C_TAG + " DESC"));

    mRecyclerView.setAdapter(adapter);
}

Second exception bcz of you are declared adapter object globally but you are creating adapter object in showRecord method locally thats why globall adapter object is null and you are accessing global object and you are getting exception

Solution 2:[2]

I faced the same problem because some fields were null in my real-time database. As soon as I cleared the fields with null values, the error got solved. Sometimes errors may not be in the code. It can be related to your data.

P.S - This is one of the solutions. (For my case)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ramyashree Shetty