the application stops working suddenly because of the NullPointerException but still can’t fix it.
and that’s what appears to me …(https://i.stack.imgur.com/saMPw.png)
i tried the try and catch method but couldn’t fix it
the application stops working suddenly because of the NullPointerException but still can’t fix it.
and that’s what appears to me …(https://i.stack.imgur.com/saMPw.png)
i tried the try and catch method but couldn’t fix it
2
Answers
Instead of using the
!!
operator onmQuestionsList
, which throws aNullPointerException
, use the Elvis operator?:
like so:I would recommend avoiding the
!!
operator in most cases, instead you can use other null safety operators such as?.
and?:
.Seems like mQuestionsList is null. you can verify that by logging or adding a debug point on line 60 and running in debug mode.
You can avoid this by adding a null check and handling the logic accordingly.
Note that the !! operator asserts that the object is NOT null and its usage is NOT recommended.
You can refer to this for better understanding of usage of null check in kotlin:
https://kotlinlang.org/docs/null-safety.html#checking-for-null-in-conditions