What happens when the following code is executed if the intent property is null? val message = intent.extras?.getString("message").toString()

Master Kotlin and Android with our engaging quizzes. Test your knowledge with multiple choice questions and detailed explanations. Prepare thoroughly for your Android development journey!

In this scenario, when the intent property is null, the code being executed will not crash because of the safe call operator ?.. Safe calls help prevent null pointer exceptions by allowing you to safely access properties or methods on a nullable type. In this case, if intent is null, the expression intent.extras will also be null, but because of the safe call operator, the code will short-circuit and not attempt to access extras, thereby avoiding a crash.

Instead, the expression intent.extras?.getString("message") will evaluate to null, leading to the next part of the code, which converts that to a String using toString(). Since toString() on null returns the string "null", the final value of message will simply be the string "null" rather than causing a runtime exception.

This means that the app will run smoothly without crashing, confirming that access through the safe call operator makes it possible to handle potentially null values without an issue.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy