即使列表已初始化并且传递的值不为空,“.contains”也会返回空指针异常

Vai*_*gal 1 android kotlin

安卓工作室截图

堆栈跟踪图像

现在工作图片

上面的屏幕截图有问题代码,待办事项注释将引导您找到问题所在。第 34 行是故障点。我已经在不同的IDE中分别尝试了带有空列表的代码,它运行得很好。这是我测试过的代码,运行良好 -

fun main() {
    val dList = mutableListOf<String>()
    val newString = "hello"
    if (dList.contains(newString)){
        print("contains")
    }else{
        print("does not contain")
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,空列表不是问题。我还尝试复制粘贴我正在关注的教程中的代码[我目前正在学习 kotlin 中的 android studio 基础知识],但这也不起作用。我什至不知道该尝试什么了。我什至在这里搜索了错误。这是针对具有初始化问题的java。我的没有那个。为了安全起见,我再次执行了前面的步骤,看看是否遗漏了什么。没有找到任何东西。所以,我被困住了。下面给出了屏幕截图中的代码[还包括注释掉的代码] -

package com.example.android.unscramble.ui.game

import android.util.Log
import androidx.lifecycle.ViewModel

class GameViewModel : ViewModel() {
    val TAG = "GameViewModel"

    init {
        Log.d(TAG, "View Model initialised")
        getNextWord()
    }
    private var _score = 0
    private var _currentWordCount = 0
    private lateinit var _currentScrambledWord: String
    val currentScrambledWord: String get() = _currentScrambledWord
    private var wordsList: MutableList<String> = mutableListOf()
    lateinit var currentWord: String

    override fun onCleared() {
        super.onCleared()
        Log.d("GameViewModel", "game view model destroyed")
    }

    private fun getNextWord() {
        currentWord = allWordsList.random()     //todo - is getting assigned
        Log.d(TAG,"current word = ${currentWord}")  //todo - current word isn't null
        val tempWord = currentWord.toCharArray()
        tempWord.shuffle()
        while (String(tempWord).equals(currentWord, false)) {
            tempWord.shuffle()
        }
        Log.d(TAG,"point - 1")  //todo - gets executed
        if (wordsList.contains(currentWord)){       //todo - point of failure
            Log.d(TAG,"point - 2")      //todo - not getting executed
            getNextWord()
        } else {
            Log.d(TAG,"point - 3")      //todo - not getting executed
            _currentScrambledWord = String(tempWord)
            ++_currentWordCount
            wordsList.add(currentWord)
        }
    }//todo - there isn't anything executable below. all commented out
    /*
    fun getNextWord() {
        currentWord = allWordsList.random()
        Log.d(TAG, "current word = ${currentWord}")
        if (wordsList.contains(currentWord)) {
            Log.d(TAG, "step - 1")
            getNextWord()
        }
        else {
            Log.d(TAG, "step - 2")
            val tempWord = currentWord.toCharArray()
            while (String(tempWord) == currentWord) {
                tempWord.shuffle()
            }
            Log.d(TAG, "step - 3")
            wordsList.add(currentWord)
            _currentScrambledWord = String(tempWord)
            _currentWordCount++
        }
    }
    */
}
Run Code Online (Sandbox Code Playgroud)

a_l*_*ody 6

我相信发生这种情况是因为代码的顺序,该init块在其他变量初始化之前执行。如果您要将 init 块移动到方法中:

fun foo() {
   Log.d(TAG, "View Model initialised")
   getNextWord()
}
Run Code Online (Sandbox Code Playgroud)

并且foo在创建视图模型的实例后调用,这应该可以解决问题。

或者,将变量声明移到init 块之前也可能有效。

我想,类似类型的问题在这里