Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Kotlin will not pass a nullable String to a parameter that needs a String

ErrorKotlin reports a type mismatch where a String? is passed as a String

Short 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.

  • Applies to Kotlin 2.x, Android Studio Otter, AGP 8.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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-null
  2. 2Match what you see, then jump to its fix

    1. 1The error points at the call where the String? is passed inThe value really can be null and nothing handles that caseFix
    2. 2You wrote if (x != null) and the error remains inside the blockThe null check is there but the compiler does not trust itFix
    3. 3You are sure null cannot happen at this pointThe value is known to be non-null but is declared as nullableFix

What does the error look like?

  • Kotlin reports a type mismatch where a String? is passed as a String
  • Only 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 error

2Fix

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 assembleDebug

Confirmed by kotlinlang.org

Did this fix it?

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 assembleDebug

Confirmed by kotlinlang.org

Did this fix it?

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 installDebug

Confirmed by kotlinlang.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.