Problems sending ArrayList in intent

I’ve created my first custom BaseAdapter and am attempting to pull data from AWS dynamoDB(This works) and place into an object ArrayList, send threw an intent and then a ListView in the next activity. For some reason the ArrayList gives a null pointer error in activity 2.

Activity 1:

//fyi threw a log I can confirm floridaArray is not null   
 mbundle = new Bundle();
                mbundle.putParcelableArrayList("floridaArray", floridaArray);
                intent.putExtra("mBundle", mbundle);

Activity 2:

// Now the ArrayList is null
 ArrayList<FloridaStatutes> floridaArray = intent.getParcelableArrayListExtra("floridaArray");

Logcat:

12-09 14:44:38.279 5316-5316/com.ronaldpitt.floridalaw E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.ronaldpitt.floridalaw, PID: 5316
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ronaldpitt.floridalaw/com.ronaldpitt.floridalaw.views.TitleTwoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                             at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                             at android.os.Looper.loop(Looper.java:164)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
                                                                             at com.ronaldpitt.floridalaw.views.TitleTwoActivity.onCreate(TitleTwoActivity.java:48)
                                                                             at android.app.Activity.performCreate(Activity.java:6999)
                                                                             at android.app.Activity.performCreate(Activity.java:6990)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                                             at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                             at android.os.Looper.loop(Looper.java:164) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 

Answer

Your way of sending Intent is not correct, the Intent itself has method putParcelableArrayListExtra.

to get your code works you should use

getIntent().getBundleExtra("mBundle").getParcelableArrayListExtra("floridaArray");

or

intent.getExtras().getParcelableArrayListExtra("floridaArray");

Leave a Reply

Your email address will not be published. Required fields are marked *