Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

kotlin

Hi I am creating a model class using @Parcelize annotation but I am getting an error when server sends image null in the response.

@Parcelize
data class EpisodeResponse(

    @Json(name="summary")
    val summary: String? = null,

    @Json(name="number")
    val number: Int? = null,

    @Json(name="airtime")
    val airtime: String? = null,

    @Json(name="image")
    val image: Image? = null,

    @Json(name="_links")
    val links: Links? = null,

    @Json(name="airdate")
    val airdate: String? = null,

    @Json(name="name")
    val name: String? = null,

    @Json(name="season")
    val season: Int? = null,

    @Json(name="runtime")
    val runtime: Int? = null,

    @Json(name="airstamp")
    val airstamp: String? = null,

    @Json(name="id")
    val id: Int? = null,

    @Json(name="url")
    val url: String? = null
): BaseResponse(), Parcelable

Image.kt

@Parcelize
data class Image(

        @Json(name = "original")
        val original: String? = null,

        @Json(name = "medium")
        val medium: String? = null
) : Parcelable

Error Logs:

   02-24 06:51:10.720 7178-7178/com.abc.android E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.abc.android, PID: 7178
   java.lang.NullPointerException: Attempt to invoke interface method 'void android.os.Parcelable.writeToParcel(android.os.Parcel, int)' on a null object reference
      at com.abc.android.data.model.episode.EpisodeResponse.writeToParcel(EpisodeResponse.kt:0)
      at android.os.Parcel.writeParcelable(Parcel.java:1416)
      at android.os.Parcel.writeValue(Parcel.java:1322)
      at android.os.Parcel.writeArrayMapInternal(Parcel.java:665)
      at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
      at android.os.Bundle.writeToParcel(Bundle.java:1079)
      at android.os.Parcel.writeBundle(Parcel.java:690)
      at android.os.Parcel.writeValue(Parcel.java:1319)
      at android.os.Parcel.writeArrayMapInternal(Parcel.java:665)
      at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
      at android.os.Bundle.writeToParcel(Bundle.java:1079)
      at android.os.Parcel.writeBundle(Parcel.java:690)
      at android.content.Intent.writeToParcel(Intent.java:7793)
      at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2639)
      at android.app.Instrumentation.execStartActivity(Instrumentation.java:1507)
      at android.app.Activity.startActivityForResult(Activity.java:3917)
      at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
      at android.app.Activity.startActivityForResult(Activity.java:3877)
      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
      at android.app.Activity.startActivity(Activity.java:4200)
      at android.app.Activity.startActivity(Activity.java:4168)
      at com.abc.android.ui.episode.EpisodeActivity$onEpisodeResult$1.onClick(EpisodeActivity.kt:43)
      at com.abc.android.ui.episode.EpisodeAdapter$ViewHolder$bindItems$1.onClick(EpisodeAdapter.kt:31)
      at android.view.View.performClick(View.java:5198)
      at android.view.View$PerformClick.run(View.java:21147)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5417)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   02-24 06:51:10.748 5961-5961/? E/EGL_emulation: tid 5961: eglCreateSyncKHR(1215): error 0x3004 (EGL_BAD_ATTRIBUTE)

Does anyone know how to handle this in kotlin ?

like image 764
N Sharma Avatar asked Dec 23 '22 09:12

N Sharma


1 Answers

Try to check BaseResponse maybe some field of them not annotated as nullable

val value: String? = null

as a result you caught this Exception.

like image 196
a23sokolov Avatar answered Dec 28 '22 08:12

a23sokolov