1

I have created an arraylist, and a ListView. I intend to iterate through the ListView, and checking whether they are checked or not, and then add the object (using ListView.getItem()) to my arraylist. however I get a NullPointerException. The ArrayList people_selected, is declared at top of class like this:

ArrayList<PeopleDetails> selected_people;

And my code:

for (int i = 0; i < people_list.getCount(); i++) {
              item_view = people_list.getAdapter().getView(i, null, null);
                chBox = (CheckBox) item_view.findViewById(R.id.checkBox);//your xml id value for checkBox.
                if (chBox.isChecked()) {
                    selected_people.add((PeopleDetails) people_list.getItemAtPosition(i));
                }
            }

        for(PeopleDetails person : selected_people){

            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(person.number, null, sms_message, null, null);        
            }
        ///and now need to write party to file.

I get an error at the line

for(PeopleDetails person : selected_people)

Saying "NullPointerException" . I take this to mean that the arraylist is null, and cannot figure out why it should be null. Have I declared it wrong at the top of my class? Or is my selection and add method faulty?

abhi
  • 1,760
  • 1
  • 24
  • 40
Swedish Architect
  • 389
  • 3
  • 5
  • 23
  • 1
    Add "ArrayList people_selected = new ArrayList(); – rootpanthera Jan 25 '14 at 11:10
  • your use different variable names for arraylist in declatetion and adding to item part? – Mustakimur Khandaker Jan 25 '14 at 11:12
  • Declaring a reference variable to an object does nothing more than reserve space for you to set it to a reference of a real object *at some point in the future*. Until you set it to a reference, it points to nothing, or null, which causes the null pointer exception when you try to dereference it (purists, I know the unitialised null references points to a null object but for the sake of a simple comment....) – Simon Jan 25 '14 at 11:24

6 Answers6

15
ArrayList<PeopleDetails> people_selected;

You declared and never initialized. Just initialize it before using it. Otherwise NullPointerException.

Try to initialize it

ArrayList<PeopleDetails> people_selected= new ArrayList<PeopleDetails>();
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
7

you missed

people_selected = new ArrayList<PeopleDetails>();

you have declared it but not intialized.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
2

The code shows that you declare the variable but doesn't show that you initialize it, like this:

people_selected = new ArrayList<PeopleDetails>();
laalto
  • 150,114
  • 66
  • 286
  • 303
2

You declared people_selected but you're using selected_people?!
Which is never filled...

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
0

The enhanced for loop is roughly of this structure

for (Iterator<T> i = someList.iterator(); i.hasNext();) {

}

An un-initialized Collection ArrayList<PeopleDetails> selected_people; refers to null.

If you start an enhanced for loop on an un-initialized Collection it will throw NullPointerException because of the fact that it calls the iterator someList.iterator() on the null reference.

On the other hand if you have an initialized Collection like so

ArrayList<PeopleDetails> selected_people = new ArrayList<>();

you will notice that the enhanced for loop doesn't throw any NullPointerException because of the fact that someList.iterator() now returns an iterator and i.hasNext() returns false just so that the loop doesn't proceed.

PS: The enhanced for loop skeleton is taken from here.

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
0

The error occurs because you haven't initialised the array

Adding this solved my issue

selected_people = new ArrayList<PeopleDetails>();
phoenixstudio
  • 1,776
  • 1
  • 14
  • 19