1

I am getting first time arraylist is null but i am checking this if arraylist is empty or not but app is crashed.

this arraylist is in my adapter workerAdapter.getSelectedList();

Here is my code

  btn_update_staff_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<WorkerItem> arrayList = workerAdapter.getSelectedList();

                List<String> chipValues = nacho_text_view.getChipValues();

                if (!arrayList.isEmpty() && arrayList != null) {
                    List<String> emp_id_member = new ArrayList<>();
                    for (int i = 0; i < chipValues.size(); i++) {

                        String emp_id = arrayList.get(i).getWorker_emp_id();
                        emp_id_member.add(emp_id);
                    }
                } else if (!worker_list_temp.isEmpty()) {
                    List<String> emp_id_member = new ArrayList<>();
                    for (int i = 0; i < chipValues.size(); i++) {
                        String emp_id = worker_list_temp.get(i).getWorker_emp_id();
                        emp_id_member.add(emp_id);
                    }
                } else {

                    Toast.makeText(context, "Please Select Employee", Toast.LENGTH_SHORT).show();
                }


            }
        });
Asteroid
  • 718
  • 7
  • 21
Mind Werx
  • 77
  • 12
  • 1
    first check if the `list != null` then check for empty – Ashutosh Sagar Feb 25 '19 at 05:21
  • Replace `if (!arrayList.isEmpty() && arrayList != null)` to ` if (arrayList != null && !arrayList.isEmpty())` .. – ADM Feb 25 '19 at 05:21
  • java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.mindwerx.in.Adapter.WorkerAdapter.getSelectedList()' on a null object reference getting this error – Mind Werx Feb 25 '19 at 05:28

4 Answers4

4

Change

if (!arrayList.isEmpty() && arrayList != null) {

to

if (arrayList != null && !arrayList.isEmpty()) {

You want to make sure that the list is not null before you run a method #isEmpty() on it.

ADM
  • 20,406
  • 11
  • 52
  • 83
Aman Arora
  • 109
  • 1
  • 11
  • java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.mindwerx.in.Adapter.WorkerAdapter.getSelectedList()' on a null object reference getting this error – Mind Werx Feb 25 '19 at 05:28
  • it can not goes to this validation it crashes on List arrayList = workerAdapter.getSelectedList(); this line – Mind Werx Feb 25 '19 at 05:29
  • @MindWerx the reason your app is crashing is not because the list is null, but the adapter that you're using the list from is null. What you should do is `if (null == workerAdapter) { Toast.makeText(getActivity(), "Adapter is null", Toast.LENGTH_LONG).show(); return; }` Add this to the start of the function. – Aman Arora Feb 25 '19 at 05:29
0

You should initialize arraylists before adding data in them

List<WorkerItem> arrayList = new ArrayList<WorkerItem>();

        List<String> chipValues = new ArrayList<String>();
Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

In this line: if (!arrayList.isEmpty() && arrayList != null) { if arrayList is null then isEmpty() call will throw a NullPointerException because you're calling the method first before checking if null.

So, do this instead: if (arrayList != null && !arrayList.isEmpty() ) { This will first check if arrayList is null, if so, then the part after && will not be executed because you already got false for one of the operands of logical and operation.

false and something = false

Raihanul
  • 91
  • 1
  • 5
0

There is a difference Between Null and empty. null means the arraylist is not initialized and isEmpty() is used to check that is there any item is present in array or not. if the arraylist size is 0 then it return false else true