Searching through a listview populated by firebase

obtain the error : "E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myhouse, PID: 4134 java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:389) at android.widget.AdapterView.checkFocus(AdapterView.java:759) at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:882) at android.widget.AbsListView$AdapterDataSetObserver.onInvalidated(AbsListView.java:6564) at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:50) at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:63) at android.widget.ArrayAdapter$ArrayFilter.publishResults(ArrayAdapter.java:620) at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:284) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)"

Code is

public class contractorList extends Fragment {



    private ListView itemListView;
    private SearchView search_View;
    private ArrayList<String> arrayContractor;
    private ArrayAdapter<String> adapter;
    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {


        View view=inflater.inflate(R.layout.fragment_home, container, false);
        itemListView=view.findViewById(R.id.list);
        retrieveData();
        initializeField();
        SearchView searchView;
        searchView=view.findViewById(R.id.search_view);
        searchView.setGravity(Gravity.CENTER);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                contractorList.this.adapter.getFilter().filter(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                contractorList.this.adapter.getFilter().filter(newText);
                return false;
            }
        });
        return view;

    }


    private void retrieveData()
    {

        DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("contractors");
        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot snapshot:dataSnapshot.getChildren())
                {
                    Contractors con= snapshot.getValue(Contractors.class);
                    String name=con.getContractorName();
                    arrayContractor.add(name);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.w("MY", "Failed to read value.", databaseError.toException());
            }
        });

    }
    private void initializeField()
    {
        arrayContractor=new ArrayList<>();
        adapter=new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,arrayContractor);
        itemListView.setAdapter(adapter);

    }
}
How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum