---
title: "Fix java.lang.NullPointerException in Kotlin and Android from its message"
description: "java.lang.NullPointerException in Kotlin comes from !!, a Java value in a non-null type, a cast of null, or a Java caller. We triggered it 6 ways on JDK 17 and show the fix for each message."
canonical: "https://logbrew.co/en/errors/nullpointerexception"
last_updated: "2026-11-10"
---

# Fix java.lang.NullPointerException in Kotlin and Android from its message

`java.lang.NullPointerException` in Kotlin comes from `!!`, a Java value in a non-null type, a cast of `null`, or a Java caller. We triggered it 6 ways on JDK 17 and show the fix for each message.

Tested on November 10, 2026 with Kotlin 2.4.0, OpenJDK 17.0.18, and logbrew-kotlin 0.2.3

By [Furkan Erday](https://github.com/furkanerday). Furkan Erday is the founder of LogBrew. He builds LogBrew with Claude Code and Codex every day.

Canonical: https://logbrew.co/en/errors/nullpointerexception

## What each `NullPointerException` message names

| What the code did | `NullPointerException` | Message names the cause |
| --- | --- | --- |
| `order!!` on a null value | Yes | No message |
| A Java value into a non-null `val` | Yes | `getProperty(...) must not be null` |
| `null as String` | Yes | `null cannot be cast to non-null type` |
| `null` from Java into a Kotlin parameter | Yes | `Parameter specified as non-null is null` |
| A method call on `null` in Java | Yes | `Cannot invoke "String.length()"` |
| A `lateinit` read before it is set | `UninitializedPropertyAccessException` | `lateinit property keg has not been initialized` |

6 runs on JDK 17 with Kotlin 2.4. A full mug in the first column means a `NullPointerException`, and in the second that its message named the cause.

## Read the message, then the first frame

### Find the line in your own code

The first stack frame in your own package names the file and the line. When the trace has a `Caused by:` section, read the frames under it first. The message then tells you which of the 5 cases below you have.

### No message, from `!!` on a null value

`!!` throws a `NullPointerException` with no message, so a logger that prints the message after a colon shows `java.lang.NullPointerException: null`. The line in the first frame holds the `!!`.

```bash
val order: String? = null
order!!.length
```

Output

```text
java.lang.NullPointerException
	at NpeKt.main$lambda$0(Npe.kt:27)
```

### `must not be null` when a Java value went into a non-null type

A Java method returns a platform type, and Kotlin checks it only when you put it in a non-null variable. `getProperty(...)` names the call that returned `null`.

```bash
val beer: String = System.getProperty("taproom.beer")
```

Output

```text
java.lang.NullPointerException: getProperty(...) must not be null
	at NpeKt.main$lambda$1(Npe.kt:30)
```

### `Parameter specified as non-null is null` when Java passed `null` into Kotlin

Kotlin checks each non-null parameter of a public function when the function is called, so a `null` from Java fails on the first line of the function. The message names the function and the parameter.

```bash
// Kotlin
fun greet(name: String): Int = name.length

// Java
NpeKt.greet(null);
```

Output

```text
java.lang.NullPointerException: Parameter specified as non-null is null: method NpeKt.greet, parameter name
	at NpeKt.greet(Npe.kt)
```

### `null cannot be cast to non-null type` after a cast of `null`

`as String` on a `null` throws. `as? String` returns `null` instead, and in our run it did, with no exception.

```bash
val value: Any? = null
value as String
```

Output

```text
java.lang.NullPointerException: null cannot be cast to non-null type kotlin.String
	at NpeKt.main$lambda$2(Npe.kt:35)
```

### `Cannot invoke` from a method call on `null` in Java code

Since JDK 15, Java names the method and the variable. `<local0>` means the class was compiled without `-g`, so the variable name is missing. Compile with `javac -g` to see it. On Android, the runtime writes `Attempt to invoke virtual method ... on a null object reference` instead.

```bash
String beer = null;
beer.length();
```

Output

```text
java.lang.NullPointerException: Cannot invoke "String.length()" because "<local0>" is null
	at Caller.lengthOfNull(Caller.java:10)
```

## Fix it in Kotlin and Android

### Replace `!!` with a fallback or a named failure

`?:` gives a default or leaves the function. `requireNotNull()` with a message fails at once and says what was missing, so the first frame and the message both point at the cause.

```bash
val order = orders[id] ?: return
val keg = requireNotNull(taps[id]) { "keg is not set for tap $id" }
```

Output

```text
java.lang.IllegalArgumentException: keg is not set for tap 3
	at NpeKt.main$lambda$6(Npe.kt:48)
```

### Declare the type nullable at the Java boundary

Give a value from Java a nullable type, and the compiler makes you handle `null` before you use it. Annotate your own Java with `@Nullable` and `@NonNull`, and Kotlin sees a real type instead of a platform type.

```bash
val beer: String? = System.getProperty("taproom.beer")
val name = beer ?: "house ale"
```

### Let a JSON parser fail at parse time, not later

A parser that builds objects by reflection can put `null` into a non-null Kotlin property, and the exception appears far from the parse. square/moshi#143 reported this in 2016. It was closed in January 2018 with the answer to use the Kotlin adapter, which rejects a missing non-null field.

### Use `lateinit` only for values set before first use

A `lateinit` property read before it is set throws `UninitializedPropertyAccessException`, not a `NullPointerException`. Check `::keg.isInitialized` first, or use `by lazy` when you can compute the value.

```bash
class Taproom {
    lateinit var keg: String
}

Taproom().keg.length
```

Output

```text
kotlin.UninitializedPropertyAccessException: lateinit property keg has not been initialized
	at Taproom.getKeg(Npe.kt:6)
```

### Send the exception with its stack to LogBrew

`captureThrowable()` in the Kotlin SDK sends the exception type, its stack frames, and its cause chain, and LogBrew groups repeats into 1 issue. The message stays out unless you pass `includeMessage = true`. In your app, pass `HttpTransport()` to `flush()` instead of the recording transport.

```bash
import co.logbrew.sdk.LogBrewAndroid
import co.logbrew.sdk.LogBrewClient
import co.logbrew.sdk.RecordingTransport

val transport = RecordingTransport.alwaysAccept()
val client = LogBrewClient.create(apiKey = System.getenv("LOGBREW_API_KEY"), sdkName = "logbrew-kotlin", sdkVersion = "0.2.3")
try {
    val beer: String = System.getProperty("taproom.beer")
    beer.length
} catch (e: NullPointerException) {
    LogBrewAndroid.captureThrowable(client, "evt_npe_1", "2026-11-10T09:00:00Z", e)
}
println(client.flush(transport))
```

Output

```text
TransportResponse(statusCode=202, attempts=1)
```

## What we saw when we tested it

We compiled 1 Kotlin file and 1 Java file with Kotlin 2.4.0 on OpenJDK 17.0.18 and triggered the exception 6 ways. Four came from Kotlin code, with `!!`, a Java value in a non-null `val`, `as String` on `null`, and a `null` that Java passed into a Kotlin parameter. The fifth was a method call on `null` in Java. The sixth, a `lateinit` read, was not a `NullPointerException` at all.

Only the `!!` case had no message. The other 4 named the call, the parameter, the type, or the method. The Java message said `<local0>` for the variable because we compiled without `-g`.

`requireNotNull()` with a message threw `IllegalArgumentException: keg is not set for tap 3` on the line we wrote it on. `as? String` returned `null` with no exception.

## Why Kotlin still throws NullPointerException

The Kotlin compiler rejects a call on a nullable type, so Kotlin code throws a `NullPointerException` only where you ask for one. The null safety docs list `!!`, an explicit `throw`, an uninitialized `this`, and Java interoperation as the only causes. A cast of `null` with `as` throws one too, as our run shows.

A value from Java has a platform type, written `String!` in compiler messages. You can put it in a `String` or a `String?`. In a `String`, Kotlin checks it at the assignment and throws `must not be null`, which is where our second run failed.

JEP 358 added the `Cannot invoke ... because ... is null` message to the JVM in JDK 14, and JDK 15 turned it on by default. Android's runtime writes its own text. google/gson#1778 shows it from an Android app, as `Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference`.

## What LogBrew records

`captureThrowable()` sent 1 issue titled `NullPointerException` at level `error`, with `exception.type` `NullPointerException`, the mechanism `android.exception`, and `handled: true`. The cause chain held 1 entry for `java.lang.NullPointerException` with 7 stack frames, each with its file, line, function, and module. The first frame was `main` in `Npe.kt` at line 58.

The message was absent, and the chain entry said `messageState: "redacted"`. With `includeMessage = true`, the issue carried `getProperty(...) must not be null` as its message. With `includeStackTrace = true`, the metadata field `throwableStackTrace` held the trace text without the message.

`flush()` answered `statusCode=202, attempts=1` through the recording transport. The same body goes to LogBrew when you pass `HttpTransport()`.

## Questions people ask

### What is a NullPointerException?

The exception the JVM throws when code uses a reference that holds `null` in a method call, a field read, an array index, or an unboxing. It extends `RuntimeException`, so no `throws` clause names it.

### Why does Kotlin throw NullPointerException if it has null safety?

Kotlin checks nullability at compile time for Kotlin types only. `!!`, `as` on `null`, a Java value you put in a non-null type, and a Java caller that passes `null` all throw at run time. Each has its own message, listed above.

### What does `java.lang.NullPointerException: null` mean?

The exception has no message. Code that prints the message after a colon writes `null` there. In Kotlin, `!!` throws exactly this exception, so look for `!!` on the line in the first frame.

### What does Parameter specified as non-null is null mean?

Java, or reflection, passed `null` into a Kotlin function whose parameter is non-null. The message names the function and the parameter. Make the parameter nullable, or stop the `null` on the caller's side.

### How do I fix NullPointerException in Android?

Read the frame under `Caused by:` that is in your package, then match the message to the cases above. On Android the runtime writes `Attempt to invoke virtual method ... on a null object reference`, and the method it names is the call on the `null`.

### Is NullPointerException a checked exception?

No. It extends `RuntimeException`, so the compiler does not make you catch it. Fix the cause instead of catching it, because a `null` that reaches a method call is a bug in the code before it.

## See every NullPointerException with its stack

The LogBrew Kotlin SDK sends each exception with its type, stack frames, and cause chain, and LogBrew groups repeats into 1 issue. Start on the generous free tier, no card needed.

[Start Free](https://logbrew.co/en)

## Sources

- [Kotlin docs, Null safety](https://kotlinlang.org/docs/null-safety.html)
- [Kotlin docs, Null-safety and platform types](https://kotlinlang.org/docs/java-interop.html#null-safety-and-platform-types)
- [JEP 358, Helpful NullPointerExceptions](https://openjdk.org/jeps/358)
- [square/moshi#143](https://github.com/square/moshi/issues/143)
- [google/gson#1778](https://github.com/google/gson/issues/1778)

## More error fixes

- [Fix the script error that hides its message, file, and line](https://logbrew.co/en/errors/script-error)
- [Fix the Failed to fetch error and tell its 3 causes apart](https://logbrew.co/en/errors/failed-to-fetch)
- [Errors we reproduced, and how to fix each one](https://logbrew.co/en/errors)
