Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Basics: Viewmodel and State in Compose :NullPointerException in the tutorial code #212

Open
bsudhanva opened this issue Sep 20, 2024 · 1 comment

Comments

@bsudhanva
Copy link

bsudhanva commented Sep 20, 2024

 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
@bsudhanva 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
@thomasem
Copy link

thomasem commented Sep 30, 2024

Seems like this is related to whether the init block is before or after usedWords is created, which should be initialized with an empty set like

    private var usedWords: MutableSet<String> = mutableSetOf()

If the init block that calls resetGame() is included before this line, it will throw a NullPointerException

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants