-2

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;
            }
        });
  • Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately - this lets those with knowledge of the language you need help with to see your question. – CertainPerformance Mar 31 '20 at 11:08

1 Answers1

0

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

Wombattle
  • 118
  • 2
  • 8