jimbob
jimbob

Reputation: 3298

ArrayAdapter error when more than one item is in the array

I have a error when I get an array which has more than one value in it the ListView... it crashes...

str_amparticipant_firstname is an array of string values. str_pmparticipant_firstname is also an array of string values.

 adapteram = new ArrayAdapter<String>(Attend.this, android.R.layout.simple_list_item_1, str_amparticipant_firstname);   

 adapterpm = new ArrayAdapter<String>(Attend.this, android.R.layout.simple_list_item_1, str_pmparticipant_firstname);   

          amattendees.setAdapter(adapteram);
          pmattendees.setAdapter(adapterpm);

The class is called Attend. the ListViews show the values in the array when there is one value in the array. If there is more than one value the app crashes. I have checked that the array is being filled correctly and it is. Hopefully the error log has the answer. The error log I am getting is below:

04-16 19:28:57.664: E/AndroidRuntime(5807): FATAL EXCEPTION: main
04-16 19:28:57.664: E/AndroidRuntime(5807): java.lang.NullPointerException
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.AbsListView.obtainView(AbsListView.java:1430)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1307)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.ListView.onMeasure(ListView.java:1127)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:701)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3202)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.View.measure(View.java:8510)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.ViewRoot.performTraversals(ViewRoot.java:871)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1921)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.os.Looper.loop(Looper.java:143)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at android.app.ActivityThread.main(ActivityThread.java:4196)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at java.lang.reflect.Method.invokeNative(Native Method)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at java.lang.reflect.Method.invoke(Method.java:507)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-16 19:28:57.664: E/AndroidRuntime(5807):     at dalvik.system.NativeStart.main(Native Method)

again the error only appears when there is more than one value in the array. I have tried to find an answer to this problem but I couldnt find any info on the error. please help! thankyou!

UPDATE: this is how the array is filled and created:

for(int i = 0; i < json_array_amparticipants.length(); i++){
                             //store all the amparticipants
                         str_amparticipant_email = new String[json_array_amparticipants.length()];
                         str_amparticipant_firstname = new String[json_array_amparticipants.length()];
                         str_amparticipant_lastname = new String[json_array_amparticipants.length()];         
                         str_amparticipant_attendingam = new String[json_array_amparticipants.length()];    
                         str_amparticipant_attendingpm = new String[json_array_amparticipants.length()];
                         //full_name_am = new String[json_array_amparticipants.length()];
                         str_amparticipant_email[i] = json_array_amparticipants.getJSONObject(i).getString("email");
                         str_amparticipant_firstname[i] = json_array_amparticipants.getJSONObject(i).getString("first_name");
                         str_amparticipant_lastname[i] = json_array_amparticipants.getJSONObject(i).getString("last_name"); 
                         str_amparticipant_attendingam[i] = json_array_amparticipants.getJSONObject(i).getString("attending_am");   
                         str_amparticipant_attendingpm[i] = json_array_amparticipants.getJSONObject(i).getString("attending_pm");
                         //full_name_am[i] = str_amparticipant_firstname[i] + str_amparticipant_lastname[i];

                     }

FIXED: I am creating new arrays every time so it still stores the value but it causes the crash... heres the updated fix. Cheers guys:

   //store all the amparticipants
             str_amparticipant_email = new String[json_array_amparticipants.length()];
             str_amparticipant_firstname = new String[json_array_amparticipants.length()];
             str_amparticipant_lastname = new String[json_array_amparticipants.length()];         
             str_amparticipant_attendingam = new String[json_array_amparticipants.length()];    
             str_amparticipant_attendingpm = new String[json_array_amparticipants.length()];


                 for(int i = 0; i < json_array_amparticipants.length(); i++){
                     //full_name_am = new String[json_array_amparticipants.length()];
                     str_amparticipant_email[i] = json_array_amparticipants.getJSONObject(i).getString("email");
                     str_amparticipant_firstname[i] = json_array_amparticipants.getJSONObject(i).getString("first_name");
                     str_amparticipant_lastname[i] = json_array_amparticipants.getJSONObject(i).getString("last_name"); 
                     str_amparticipant_attendingam[i] = json_array_amparticipants.getJSONObject(i).getString("attending_am");   
                     str_amparticipant_attendingpm[i] = json_array_amparticipants.getJSONObject(i).getString("attending_pm");
                     //full_name_am[i] = str_amparticipant_firstname[i] + str_amparticipant_lastname[i];

                 }

Upvotes: 1

Views: 449

Answers (2)

SeanPONeil
SeanPONeil

Reputation: 3910

My guess is that the bug is in here:

str_amparticipant_firstname[i] = json_array_amparticipants.getJSONObject(i).getString("first_name"); str_amparticipant_lastname[i] = json_array_amparticipants.getJSONObject(i).getString("last_name");

Is it possible that those keys are not in the JSONObject? It would explain the null pointer exception.

Upvotes: 1

dmon
dmon

Reputation: 30168

This should be outside of your loop:

                         str_amparticipant_email = new String[json_array_amparticipants.length()];
                         str_amparticipant_firstname = new String[json_array_amparticipants.length()];
                         str_amparticipant_lastname = new String[json_array_amparticipants.length()];         
                         str_amparticipant_attendingam = new String[json_array_amparticipants.length()];    
                         str_amparticipant_attendingpm = new String[json_array_amparticipants.length()];

You're recreating the arrays every time you go into the loop, erasing any previous values.

Upvotes: 4

Related Questions