You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void java.util.Set.clear()' on a null object reference
at com.example.unscramble.ui.GameViewModel.resetGame(GameViewModel.kt:41)
at com.example.unscramble.ui.GameViewModel.<init>(GameViewModel.kt:14)
The app crashes at start, and stack trace points at init method that calls resetGame() function .
Specifically at [Link to tutorial page
Got the error while i was at step 6 of Link to tutorial page
As a mitigation I had added if else for null checking, even then it produces this error
Null safety code
fun resetGame(){
if (usedWords != null)
{usedWords.clear()
_uiState.value = GameUiState(currentScrambledWord = pickRandomWordAndShuffle())
}
else _uiState.value = GameUiState(currentScrambledWord = pickRandomWordAndShuffle())
}
Log cat says-
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.Set.contains(java.lang.Object)' on a null object reference
at com.example.unscramble.ui.GameViewModel.pickRandomWordAndShuffle(GameViewModel.kt:52)
at com.example.unscramble.ui.GameViewModel.resetGame(GameViewModel.kt:43)
at com.example.unscramble.ui.GameViewModel.<init>(GameViewModel.kt:14)
And for sake of testing if i remove else block and use Unit, app launches without any error , but then there is no scrambled word displayed .
complete null safety code -
if (usedWords != null)
{usedWords.clear()
_uiState.value = GameUiState(currentScrambledWord = pickRandomWordAndShuffle())}
else Unit
The text was updated successfully, but these errors were encountered:
bsudhanva
changed the title
Android Basics: Viewmodel and State in Compose
Android Basics: Viewmodel and State in Compose :NullPointerException in the tutorial code
Sep 20, 2024
The app crashes at start, and stack trace points at init method that calls
resetGame()
function .Specifically at [Link to tutorial page
Got the error while i was at step 6 of Link to tutorial page
As a mitigation I had added if else for null checking, even then it produces this error
Null safety code
Log cat says-
And for sake of testing if i remove else block and use
Unit
, app launches without any error , but then there is no scrambled word displayed .complete null safety code -
The text was updated successfully, but these errors were encountered: