Skip to content
Null can not be a value of a non-null type

Null can not be a value of a non-null type

DodaTech 3 min read

The “Null can not be a value of a non-null type” error occurs when you try to assign null or a nullable value to a variable or parameter declared as non-null.

What It Means

Kotlin’s type system distinguishes nullable types (String?) from non-null types (String). A non-null type guarantees the value is never null, so the compiler rejects any assignment that could introduce null. This happens at compile time — unlike Java, you won’t get a runtime NullPointerException from a type violation.

Why It Happens

  • You assign null directly to a non-null variable: val name: String = null.
  • You pass a nullable value (String?) to a function expecting a non-null parameter (String).
  • You return null from a function whose return type is non-null.
  • A property is declared non-null but cannot be initialized in the constructor.
  • You access a Java method that returns null and assign it to a Kotlin non-null type.

How to Fix It

1. Use nullable types with ?

// ❌ Null can not be a value of a non-null type
val name: String = null

// ✅ Declare the type as nullable
val name: String? = null

2. Use safe calls (?.) and the Elvis operator (?:)

// ❌ Cannot assign nullable result to non-null
fun getLength(str: String?): Int {
    return str.length
}

// ✅ Safe call with Elvis operator
fun getLength(str: String?): Int {
    return str?.length ?: 0
}

3. Use lateinit for deferred initialization

// ❌ Property must be initialized or be abstract
class User {
    val name: String
}

// ✅ Use lateinit for properties set after construction
class User {
    lateinit var name: String

    fun setup() {
        name = "Alice" // Set later
    }
}

4. Convert between nullable and non-null types safely

fun process(input: String?) {
    // ❌ Type mismatch: inferred type is String? but String was expected
    val upper: String = input.toUpperCase()

    // ✅ Use a safe call with a default
    val upper: String = input?.toUpperCase() ?: "DEFAULT"
}

// Or use let for non-null execution
fun process(input: String?) {
    input?.let { safe ->
        println(safe.toUpperCase()) // safe is a non-null String here
    }
}

5. Handle Java interop with @Nullable annotations

// Java code returns @Nullable String
// ❌ Assigning nullable Java return to Kotlin non-null
val result: String = javaMethod() // might be null

// ✅ Use platform type with null check
val result: String? = javaMethod()
val safeResult: String = result ?: "fallback"
What’s the difference between lateinit and a nullable type?
lateinit is for non-null properties initialized after construction (e.g., dependency injection). Accessing it before initialization throws UninitializedPropertyAccessException. Nullable types allow null everywhere but require safe calls (?.) on every access.
Why does Kotlin make null safety a compile-time error?
To eliminate NullPointerException at runtime — the most common crash in Java. By forcing you to handle null at compile time, Kotlin guarantees that a non-null type will never throw an NPE.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro