Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am creating an array but cannot add values to it.

ArrayList<SMS>[] lists = (ArrayList<SMS>[])new ArrayList[count];

        for(int i=0;i<temp.size();i++)
        {
            String number="",id="";
            number = temp.get(i).addr;
            id = temp.get(i).thread_id;
            lists[i].add(temp.get(i));            // Problem here
        }

I am unable to add value to it

share|improve this question
    
You have to actually create the individual ArrayList objects. The new ArrayList[count] operation only creates the array itself. –  Hot Licks Mar 30 at 17:52
    
Can you please elaborate your answer with code? –  airbourne Mar 30 at 17:53

2 Answers 2

up vote 0 down vote accepted

You're creating an array of null references, so you need to initialize each of them to a new ArrayList<SMS>():

for (int i = 0; i < count; i++) {
    lists[i] = new ArrayList<SMS>();
}
share|improve this answer
int size = 9;
ArrayList<SMS>[] lists = new ArrayList[size];
for( int i = 0; i < size; i++) {
    lists[i] = new ArrayList<SMS>();
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.