Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use null with kotlin @Parcelize

Here is very simple code:

@Parcelize
data class Inner(val a: Int): Parcelable

@Parcelize
data class Test(val a: Int, val inner: Inner?): Parcelable

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun testParcel() {
        val test = Test(0, null)

        val parcel = Parcel.obtain()
        parcel.writeParcelable(test, test.describeContents())
    }
}

I have nullable property Test.inner. If it is not null, the code works fine but when it is null then I have the exception:

java.lang.NullPointerException: Attempt to invoke interface method 'void android.os.Parcelable.writeToParcel(android.os.Parcel, int)' on a null object reference

How to resolve this? Maybe I do something wrong.

like image 646
vmtrue Avatar asked Oct 22 '17 14:10

vmtrue


1 Answers

The bug has been fixed in Kotlin 1.1.60.

like image 124
makovkastar Avatar answered Oct 24 '22 08:10

makovkastar