Kotlin will not pass a nullable String to a parameter that needs a String
Kotlin reports a type mismatch where a String? is passed as a StringShort answer
Kotlin keeps String and String? as different types. A String? may be null, so it cannot go where a String is needed. Check it for null first, give it a default with the Elvis operator, return early when it is null, or assert with !! only when null is truly impossible. Mutable properties need a local copy before the check.
Fix it fast
1Find out which cause you have
Find the declaration of the value and see whether its type ends with a question mark. Hover over it in Android Studio to see the inferred type.
val input: String? = readName() // nullable fun greet(name: String) = println("Hi, $name") // needs non-null2Match what you see, then jump to its fix
- 1
The error points at the call where the String? is passed inThe value really can be null and nothing handles that caseFix - 2
You wrote if (x != null) and the error remains inside the blockThe null check is there but the compiler does not trust itFix - 3
You are sure null cannot happen at this pointThe value is known to be non-null but is declared as nullableFix
- 1
What does the error look like?
Kotlin reports a type mismatch where a String? is passed as a StringOnly safe (?.) or non-null asserted (!!.) calls are allowed on String?Null can not be a value of a non-null type String
Cause 1 of 3
The value really can be null and nothing handles that case
1Check
Look for the source of the value: an API that returns a nullable result, a map lookup, or a property declared with ?.
val input: String? = null
greet(input) // compile error2Fix
Handle null before the call. Pick the option that fits the case: a null check with a smart cast, an Elvis default, or an early return.
if (input != null) greet(input)
greet(input ?: "guest")
val name = input ?: return
greet(name)3Verify
Rebuild; the error is gone and the null case now behaves as you chose.
./gradlew assembleDebugConfirmed by kotlinlang.org
Cause 2 of 3
The null check is there but the compiler does not trust it
1Check
Smart casts work only when the compiler can be sure the value does not change between the check and the use. A var property never gets a smart cast, because other code can change it at any time; a local val always does.
class Form { var title: String? = null }
// if (form.title != null) greet(form.title) // still String?2Fix
Copy the value into a local val and check the copy.
val title = form.title
if (title != null) greet(title)3Verify
Rebuild; the smart cast now applies to the local val.
./gradlew assembleDebugConfirmed by kotlinlang.org
Cause 3 of 3
The value is known to be non-null but is declared as nullable
1Check
The not-null assertion !! turns String? into String and throws a NullPointerException when the value is null. Use it only when you can prove null is impossible.
val b: String? = "Kotlin"
println(b!!.length)2Fix
Prefer fixing the declaration so the type is String where null is not allowed. If you must assert, use !! on that one value.
val name: String = input!! // throws NullPointerException if input is null
greet(name)3Verify
Run the screen once with a real value and once with a missing one, to see that the failure case is acceptable.
./gradlew installDebugConfirmed by kotlinlang.org
How do I stop it coming back?
- Declare a type as non-null by default and add ? only where null has a real meaning.
- Handle null at the edge, where the value enters your code, so the rest of the code works with String.
- Avoid !! in production code paths; a default or early return is easier to test.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Mobile apps
Android build fails because a product flavor has no flavor dimension
The flavor 'flavor_name' is not assigned to a flavor dimension.
Android Gradle plugin refuses to build when a product flavor is not assigned to a flavor dimension.
2 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
Android build fails because two libraries ship the same META-INF file
More than one file was found with OS independent path
Two dependencies put a file at the same path inside the APK and the plugin will not guess which one wins.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fix