Reputation: 2504
I'm using string array which I'm passing to ArrayAdapter for spinner item. my array size(usually less than 10) and values are variable (I'm taking it from asset file)
if I'm using this :
String[] arr = new String[10];
protected void onCreate(Bundle savedInstanceState) {
breader = new BufferedReader(new InputStreamReader(getAssets().open(path)));
int length = Integer.parseInt(breader.readLine());
for(int i=0; i<length; i++){
arr[i]=breader.readLine();
initialize();
....
}
initialize(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> array = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, arr);
spinner1.setAdapter(array);
}
it shows NullPointerException at spinner1.setAdapter(array); line. can I reassign array length. I think its not possible.
Upvotes: 0
Views: 597
Reputation: 2140
because of you add null when no data in last portion of array. I mean there are 7 data in assest file and you move for loop 10 times so after 7 data Add there are null represent. so breader.readLine(); read null and add it to your string array and when you parse StringArray to Array Adapter there are null get on 8th position of array and nullpointer Exception fire.
So just check breader.readLine()
!= Null then add it to String of Array otherwise add some Temp text.
Or
You Also Use ArrayList instead of String[] Array. ArrayList is dynamically Arraylist you can add data to list useing array.add(yourdata).
Thats it...
Upvotes: 2