3
votes

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?

2
"attachments" is your ArrayList????Piyush
@Mzzl that isn't possible. If in were null, you wouldn't get an exception in readTypedList() because you would never get to that method. The NPE would be thrown in the method that called it.David Wasser
@Piyush Gupta Yes, attachments is my ArrayList. I probably should have written that, sorry.Phoebe
I can try it but if you look under EDIT in my original post, that seems to be working as well. Or do you think that solution is something that will crash because of something else at a later point?Phoebe
@Phoebe Your solution is also fine. No worries.David Wasser

2 Answers

9
votes

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

attachments = new ArrayList<AttachmentsData>();
3
votes

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>();