Delete function only can run half until **arrayList.remove(which_item);** show the problen

44 views Asked by At

i have a problem, code run till half showing error, this is the delete function at **arrayList.remove(which_item);** this line have problem showing

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.remove(int)' on a null object reference

Can someone tell what happen to this?

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                final int which_item = position;

                new AlertDialog.Builder(addProduct.this)
                        .setIcon(android.R.drawable.ic_delete)
                        .setTitle("Are you sure?")
                        .setMessage("Do you want to delete this item")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                **arrayList.remove(which_item);**
                                adapter.notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("No",null)
                        .show();
                return true;
            }
        });
1

There are 1 answers

1
Wombattle On

The function onClick does not know the which_item, shouldn't it be which.