skip to Main Content

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


  1. Instead of using the !! operator on mQuestionsList, which throws a NullPointerException, use the Elvis operator ?: like so:

    val question: Question = mQuestionsList?[mCurrentPosition - 1] ?: defaultQuestion
    

    I would recommend avoiding the !! operator in most cases, instead you can use other null safety operators such as ?. and ?:.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search