when I call my viewmodel it says that my parameter comes back as null even though the api call works so im wondering is the mistake happening in my viewmodel?
My viewmodel
class HomeFragmentVM(
private val spManager: SPManager,
private val repository: Repository
) : AbstractViewModel() {
private val washingMachine = MutableLiveData<Post>()
fun getWashingMachines() {
launch {
repository.getPost()
.doOnSubscribe {
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
washingMachine.value = it
spManager.saveTitleID(it.title)
}, {
})
}
}
}
The debug log:
java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter title
at com.invenium.jlp_android_test.utils.SPManager.saveTitleID(Unknown Source:2)
at com.invenium.jlp_android_test.functionalities.Home.HomeFragmentVM$getWashingMachines$1.invoke$lambda-1(HomeFragmentVM.kt:32)
at com.invenium.jlp_android_test.functionalities.Home.HomeFragmentVM$getWashingMachines$1.lambda$UfyFFA37mNg1-E8x0CGZ3EyBoEY(Unknown Source:0)
at com.invenium.jlp_android_test.functionalities.Home.-$$Lambda$HomeFragmentVM$getWashingMachines$1$UfyFFA37mNg1-E8x0CGZ3EyBoEY.accept(Unknown Source:4)
at io.reactivex.internal.observers.ConsumerSingleObserver.onSuccess(ConsumerSingleObserver.java:63)
at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:81)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:119)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
The model:
data class Post(
@SerializedName("products")
var data: List<Data>,
@SerializedName("productId")
var productid: Int,
@SerializedName("title")
var title: String
)
The model has been a little shortened as some model as not been used yet.
CodePudding user response:
Set default value for title like this:
- @SerializedName("title") var title: String = ""
after that , if your problem solved , check why title return null
CodePudding user response:
The issue is that the title
property is defined as non-nullable, but (apparently) is null or not present in the JSON.
In addition to that, the app is crashing because of the check the Kotlin compiler puts. You can see it in the stack trace:
method kotlin.jvm.internal.Intrinsics.checkNotNullParameter
This is because Gson (which I'm assuming you're using since you have @SerializedName
annotations) uses Java reflection and ignores the nullability of the Kotlin type, so it will assign the field to null even if the Kotlin property is defined as non-nullable.
To solve the crash you'll have to define your property as nullable:
var title: String?
This way, the compiler will force you to add a null check before passing title
to saveTitleID
.