8

The Code for my Parcelable Model class is below,

@Parcelize
data class Client(
    @SerializedName("id")
    var id: Int = 0,

    @SerializedName("accountNo")
    var accountNo: String? = null,

    @SerializedName("status")
    private val status: Status,

    @SerializedName("active")
    private val active: Boolean?,

    @SerializedName("activationDate")
    var activationDate: List<Int> = ArrayList(),

    @SerializedName("dateOfBirth")
    var dobDate: List<Int> = ArrayList(),

    @SerializedName("clientClassification")
    var clientClassification: @RawValue ClientClassification? = null,

    @SerializedName("clientType")
    var clientType: @RawValue ClientType? = null,

    @SerializedName("gender")
    var gender: @RawValue Gender? = null,

    @SerializedName("groups")
    var groups: @RawValue List<Group> = ArrayList()

) : Parcelable

Before converting this class to Kotlin, Java code was working fine. Why is it giving Null Pointer since I'm initializing all the List in the constructor of the data class ?

miPlodder
  • 795
  • 8
  • 18
  • There are various data types which are nullable in your Client model, like `ClientClassification` `ClientType` `Gender` so although you are initializing lists, still you can get Null in these Nullable entities. – Aseem Sharma Jun 22 '18 at 12:11
  • I don't think this is a problem as other model classes are working fine. Has this ever occured to you ? – miPlodder Jun 22 '18 at 12:41
  • Yes, usually NPE is been thrown if you are calling some nullable type with this '!!' operator, as I am also using nullable fields in models, but never faced NullPointerException. – Aseem Sharma Jun 22 '18 at 12:45
  • I'm not calling any nullable object using methods. The error is in auto-generated code by @Parcelize Annotation inside writeToParcel method. – miPlodder Jun 22 '18 at 13:27
  • @miPlodder I am also facing this with Parcelize maybe some issue with the kotlin plugin. – Insane Developer Oct 21 '20 at 16:35
  • Any solution for the issue? – JustKhit Dec 30 '20 at 18:01
  • @InsaneDeveloper I have searched for the solution and finally end up with https://bladecoder.medium.com/a-study-of-the-parcelize-feature-from-kotlin-android-extensions-59a5adcd5909. Actually there isn't an issue from Kotlin plugin but some fields on my data class are null. – JustKhit Dec 31 '20 at 04:13

0 Answers0