Phoebe
Phoebe

Reputation: 179

NullPointerException when trying to read parcel ArrayList<Object>

I've got a class Message that implements Parcelable. It partly contains an ArrayList, AttachmentsData is also one of my classes. When I'm trying to pass a Messageobject from my MainActivity to another activity I get NullPointerException when trying to read this ArrayList. AttachmentsData also implements Parcelable.

The error:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.incentive/com.example.incentive.Comments}: java.lang.NullPointerException 1at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) android.app.ActivityThread.access$600(ActivityThread.java:127) android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) android.os.Handler.dispatchMessage(Handler.java:99) android.os.Looper.loop(Looper.java:137) android.app.ActivityThread.main(ActivityThread.java:4441) java.lang.reflect.Method.invokeNative(Native Method) java.lang.reflect.Method.invoke(Method.java:511) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException android.os.Parcel.readTypedList(Parcel.java:1638) com.example.incentive.messagemodel.Message.readFromParcel(Message.java:88) com.example.incentive.messagemodel.Message.(Message.java:40) com.example.incentive.messagemodel.Message$1.createFromParcel(Message.java:103) com.example.incentive.messagemodel.Message$1.createFromParcel(Message.java:1) android.os.Parcel.readParcelable(Parcel.java:1992) android.os.Parcel.readValue(Parcel.java:1854) android.os.Parcel.readMapInternal(Parcel.java:2094) android.os.Bundle.unparcel(Bundle.java:223) android.os.Bundle.getParcelable(Bundle.java:1158) com.example.incentive.Comments.onCreate(Comments.java:70) android.app.Activity.performCreate(Activity.java:4465) android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)

In "public void writeToParcel(Parcel dest, int flags)" when I'm writing the ArrayList:

dest.writeTypedList(attachments);

And what seems to be causing the error, in "private void readFromParcel(Parcel in)":

in.readTypedList(attachments, AttachmentsData.CREATOR); 

I've tried finding different ways of solving it and have tried some versions, one being:

in.readList(attachments, AttachmentsData.class.getClassLoader());

But the app crashes.. Does anyone have a clue what I'm doing wrong? Or rather, I'm obviously writing or reading the ArrayList in a way that's not correct, but what is the right way?

Thanks, Kristina

EDIT I may just have solved it, I changed the in to

attachments = in.createTypedArrayList(AttachmentsData.CREATOR);

Seems to be working! I guess it may have the same effect as David Wasser's answer?

Upvotes: 4

Views: 3549

Answers (2)

Piyush
Piyush

Reputation: 18933

May be you have just declared your ArrayList< AttachmentsData>() globally. You have not initialized it.

So you have to initialize that before calling readTypedList()

And Globally declared as a

ArrayList<AttachmentsData> attachments ;

Like:

attachments = new ArrayList<AttachmentsData>(); 

Upvotes: 3

David Wasser
David Wasser

Reputation: 95568

You need to set the attachments variable to a list before you call readTypedList(), like this:

attachments = new ArrayList<AttachmentsData>();

Upvotes: 9

Related Questions