Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin's @Parcelize throws NPE on writeToParcel()

I have this set of data classes, all in the same file:

@Parcelize data class Response(val RecordView: RecordView):Parcelable

@Parcelize data class RecordView(val Records: List<Records>):Parcelable

@Parcelize data class Gid(val Value: String):Parcelable

@Parcelize data class Features(val Fields: List<Fields>, val FeatureName: String, val Title: String):Parcelable

@Parcelize data class Fields(val Label: String, val Value: String, val FieldName: String, val Features: List<Features>):Parcelable

@Parcelize data class Records(val Gid: Gid, val ImageIds: List<String>, val Fields: List<Fields>, val Features: List<Features>):Parcelable

I then send the Response object as an intent for the next activity:

val record = intent?.getParcelableExtra<Records>(RECORD)

but the next activity then throws this:

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.Collection.size()' on a null object reference
                                                                                    at entities.Features.writeToParcel(Features.kt)
                                                                                    at entities.Records.writeToParcel(Records.kt)
                                                                                    at android.os.Parcel.writeParcelable(Parcel.java:1496)
                                                                                    at android.os.Parcel.writeValue(Parcel.java:1402)
                                                                                    at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
                                                                                    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
                                                                                    at android.os.Bundle.writeToParcel(Bundle.java:1157)
                                                                                    at android.os.Parcel.writeBundle(Parcel.java:764)
                                                                                    at android.content.Intent.writeToParcel(Intent.java:8687)
                                                                                    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3082)
                                                                                    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
                                                                                    at android.app.Activity.startActivityForResult(Activity.java:4225)

This same process works when I just send a more basic class (just Strings in the parameters), using the same @Parcelize.

And yes, I turned on experimental on my gradle file.

Any ideas? Thanks

like image 601
TooManyEduardos Avatar asked Jan 19 '18 23:01

TooManyEduardos


1 Answers

I think your API should not response -> Null Object

because data intent can't write Null (write to parcel)

when u use @Parcelize

https://youtrack.jetbrains.com/oauth?state=%2Fissue%2FKT-20032

like image 82
Chaiwat Rachawat Avatar answered Sep 23 '22 22:09

Chaiwat Rachawat