1

I have a activity, in which it has a button, on click of the button the app crashes problem is on line adapter.remove(list.get(i));

Logcat detail mention NullPointerException on the given line adapter.remove(list.get(i));

   package com.example.veeresh.myphotogallery;

   import android.app.ListActivity;
   import android.os.Bundle;
   import android.util.SparseBooleanArray;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.widget.ArrayAdapter;
   import android.widget.Button;
   import android.widget.EditText;

   import java.util.ArrayList;

   public class MainActivity extends ListActivity {

/** Items entered by the user is stored in this ArrayList variable */
ArrayList list = new ArrayList();

/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** Setting a custom layout for the list activity */
    setContentView(R.layout.activity_main);

    /** Reference to the add button of the layout main.xml */
    Button btn = (Button) findViewById(R.id.btnAdd);

    /** Reference to the delete button of the layout main.xml */
    Button btnDel = (Button) findViewById(R.id.btnDel);

    /** Defining the ArrayAdapter to set items to ListView */
    adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
    adapter.add("Item 1");

    /** Defining a click event listener for the button "Add" */
    OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText edit = (EditText) findViewById(R.id.txtItem);
            list.add(edit.getText().toString());
            edit.setText("");
            adapter.notifyDataSetChanged();
        }
    };

    /** Defining a click event listener for the button "Delete" */
    OnClickListener listenerDel = new OnClickListener() {
        @Override
        public void onClick(View v) {
            /** Getting the checked items from the listview */
            SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
            int itemCount = getListView().getCount();

            for(int i=itemCount-1; i >= 0; i--)
            {
                if(checkedItemPositions.get(i)){
                    adapter.remove(list.get(i));
                }
            }
            checkedItemPositions.clear();
            adapter.notifyDataSetChanged();
        }
    };

    /** Setting the event listener for the add button */
    btn.setOnClickListener(listener);

    /** Setting the event listener for the delete button */
    btnDel.setOnClickListener(listenerDel);

    /** Setting the adapter to the ListView */
    setListAdapter(adapter);
}
}
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
bappi bazzi
  • 225
  • 1
  • 3
  • 12
  • post your logcat error here... may be your adapter or list should be null – Priyank Patel Sep 08 '15 at 14:31
  • java.lang.NullPointerException at com.example.veeresh.myphotogallery.MainActivity$2.onClick(MainActivity.java:61) – bappi bazzi Sep 08 '15 at 14:35
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Albireo Mar 07 '16 at 08:56

2 Answers2

2

Your adapter contains a list, and your MainActivity contains another list.

You are adding items to your Adapter, however this does not add items to the list in your Mainactivity. The list in your Adapter contains "Item 1", but the list in MainActivity does not.

OnClick you are trying to retrieve an item from the list in your MainActivity, which doesn't contain the item you are trying to retrieve, hence the NullpointerException

You should add or remove items from the list and then call adapter.notifyDataSetChanged();

Try:

//add an item
list.add("item 1");
adapter.notifyDataSetChanged();



//remove an item
list.remove(i);
adapter.notifyDataSetChanged();
Frank D.
  • 1,306
  • 1
  • 13
  • 23
0

I believe you should change your problematic line with this:

adapter.remove(list.getAdapter().getItem((checkedItemPositions.keyAt(i)));
Alberto Garrido
  • 485
  • 2
  • 6
  • 15