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?
in
were null, you wouldn't get an exception inreadTypedList()
because you would never get to that method. The NPE would be thrown in the method that called it. – David Wasser