Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Android 4.0+ I have 10 spinners, have loaded into an array I am trying to associate with 10 ArrayAdapters, using an array of ArrayAdapters. Not working...

ArrayAdapter<CharSequence>[] adpPPI;

for (int ppiCntr = 0; ppiCntr < 10; ppiCntr++) {

//NOTE 1: next statement gets resource id, works ok
    int res_array = getResources().getIdentifier(
        "com.my:array/ppi" + Integer.toString(ppiCntr + 1) + "_array", null, null);

// NOTE 2: creating the array adapter provides null pointer error. 
// because I need to initialize it somehow? how? 
    adpPPI[ppiCntr] = ArrayAdapter.createFromResource(
        getApplicationContext(), res_array, R.layout.psm_simple_spinner_item);
}
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

That's because you never initialise adpPPI.

Inbetween the array declaration ...

ArrayAdapter<CharSequence>[] adpPPI;

... and trying to assign values to its indices ...

adpPPI[ppiCntr] = ArrayAdapter.createFromResource(...);

... you never create the array.

In short, you'll probably want to add something like this somewhere inbetween the two:

adpPPI = new ArrayAdapter[10];

Hint: make sure to do that somewhere above the for loop.

Note: You can't create arrays of a generic type in Java, so you'll have to use the raw type (or perhaps better said: make the initialisation of the array type-less). Alternatively, you could use a Collection/List in stead of an array.

share|improve this answer
2  
I tried: adpPPI = new ArrayAdapter<CharSequence>[10]; before the for loop, but I get the error in Eclipse: "Cannot create a generic array of ArrayAdapter<CharSequence>" –  eCubeH 21 hours ago
 
You might want to swap out ppiCntr with an actual number in case the variable doesn't have a sensible value before the for loop. Updated the answer to illustrate that. –  MH. 21 hours ago
 
Ah yes, silly me. Java doesn't do array of a generic type. In stead, you're going to have to use the 'raw' type. Eclipse will warn you about that, but if you know what you're doing, you should be fine. I'll update the answer again. Alternatively, use a Collection/List in stead of an array. –  MH. 21 hours ago
 
Thanks so much. Works! –  eCubeH 20 hours ago
add comment
List<ArrayAdapter<CharSequence>> adpPPI = new Vector<ArrayAdapter<CharSequence>>();

for (int ppiCntr = 0; ppiCntr < 10; ppiCntr++) {

//NOTE 1: next statement gets resource id, works ok
    int res_array = getResources().getIdentifier(
        "com.my:array/ppi" + Integer.toString(ppiCntr + 1) + "_array", null, null);

// NOTE 2: creating the array adapter provides null pointer error. 
// because I need to initialize it somehow? how? 
    adpPPI.add(ArrayAdapter.createFromResource(
        getApplicationContext(), res_array, R.layout.psm_simple_spinner_item));
}

Try the above code, You missed initializing your array.

share|improve this answer
 
get the error in eclipse: Eclipse: "Cannot create a generic array of ArrayAdapter<CharSequence>" –  eCubeH 21 hours ago
 
Oh, Sure please check the edited answer –  Hardik4560 20 hours ago
 
Thanks so much! I decided to stick with the 'raw' type arrayadapter for now. But I understand what you have suggested and am happy to see this elegant approach too. –  eCubeH 20 hours ago
add comment

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.