“Parameter specified as non-null is null” means Java passed null into a non-null Kotlin parameter. The usual trigger is a Kotlin override of a Java callback. The Java interface has no nullability annotations, so Kotlin sees platform types. It lets you pick String or String? without a warning. If you pick String, the compiler adds a null check at the top of the function. That check runs before your first line. So an if (error != null) guard inside the body never gets a chance. The crash usually shows up only on the failure path. That is where the SDK sends null data. The crash also hides the real error, such as a declined card. The fix is to declare every unannotated callback parameter nullable and decide what each null means. In an interview, name both causes. The platform type let the wrong signature compile. Then the generated check rejected the null at runtime.
Parameter specified as non-null is null only when the card is declined
A checkout screen calls a payment SDK written in Java. The SDK reports the result through a two-argument callback. On success it passes a receipt ID and a null error. On failure it passes a null receipt ID and the error. Here is a minimal version of that SDK.
// PaymentCallback.java
package sdk;
public interface PaymentCallback {
void onResult(String receiptId, Throwable error);
}
// PaymentClient.java (same package)
public class PaymentClient {
public void charge(int cents, PaymentCallback callback) {
if (cents > 0) {
callback.onResult("rcpt_" + cents, null);
} else {
callback.onResult(null, new IllegalArgumentException("card_declined"));
}
}
}
The Kotlin side looks careful. The developer knew error could be null, so it is Throwable?. They assumed a receipt always arrives, so receiptId is String. The body even checks the error first.
class CheckoutViewModel {
var status = "idle"
fun pay(cents: Int) {
PaymentClient().charge(cents, object : PaymentCallback {
override fun onResult(receiptId: String, error: Throwable?) {
if (error != null) {
status = "failed: ${error.message}"
return
}
status = "paid: $receiptId"
}
})
}
}
We compiled this with Kotlin 2.4.20 and ran it on OpenJDK 25. A plain class stands in for the Android ViewModel. One call paid 1999 cents. A second call paid 0 to force a decline.
paid: rcpt_1999 java.lang.NullPointerException: Parameter specified as non-null is null: method CheckoutViewModel$pay$1.onResult, parameter receiptId
The success path works, so a happy-path test passes. The decline crashes. The card_declined message never reaches the screen. On Android, an uncaught exception like this one crashes the app. Two separate rules stack to produce it.
A platform type lets you declare the parameter non-null
PaymentCallback has no nullability annotations. Kotlin can’t know whether receiptId may be null. The Java interop page calls such types platform types. Kotlin relaxes null-safety checks for them. You choose the type. The compiler trusts your choice.
So both receiptId: String and receiptId: String? compile as valid overrides. Neither gets a warning, because the compiler has no information to warn with. Only the SDK’s behavior says that a decline sends a null receipt.
Annotations change this. The same page lists the annotation packages Kotlin reads, including androidx.annotation, JetBrains annotations and JSpecify. Since Kotlin 2.1.0, released in November 2024, a JSpecify nullability mismatch is an error by default. We added JSpecify’s @Nullable to both callback parameters and recompiled. The receiptId: String override failed with “‘onResult’ overrides nothing.” The compiler suggested onResult(p0: String?, p1: Throwable?) instead. That is why this crash mostly comes from older or unannotated Java SDKs.
Kotlin checks non-null parameters before the body runs
Java can pass null into any parameter. The Kotlin docs on calling Kotlin from Java say so directly. For that reason, Kotlin generates runtime checks for all public functions that expect non-nulls. An override of a Java interface method is public. So onResult gets a check on receiptId at its very first instruction.
The developer’s guard reads error. That parameter is nullable, so it has no generated check. But the check on receiptId runs first. On the decline path it sees null and throws. The guard and the failure branch never execute.
The exception type has changed over time. Since Kotlin 1.4, released in August 2020, these checks throw java.lang.NullPointerException. Earlier versions threw IllegalArgumentException with the same message. Older crash reports and search results still show that type. The message text is the same in both, so search for the message.
Declare every unannotated callback parameter nullable, then map the nulls
Make both parameters nullable. Then turn the pair into one result type. The rest of the code never sees two nullable values.
sealed interface PayResult {
data class Paid(val receiptId: String) : PayResult
data class Failed(val cause: Throwable) : PayResult
}
fun toResult(receiptId: String?, error: Throwable?): PayResult = when {
error != null -> PayResult.Failed(error)
receiptId != null -> PayResult.Paid(receiptId)
else -> PayResult.Failed(IllegalStateException("SDK returned neither receipt nor error"))
}
class CheckoutViewModel {
var status = "idle"
fun pay(cents: Int) {
PaymentClient().charge(cents, object : PaymentCallback {
override fun onResult(receiptId: String?, error: Throwable?) {
status = when (val r = toResult(receiptId, error)) {
is PayResult.Paid -> "paid: ${r.receiptId}"
is PayResult.Failed -> "failed: ${r.cause.message}"
}
}
})
}
}
The same run now prints both outcomes.
paid: rcpt_1999 failed: card_declined
The else branch covers a case the signature allows. A Java SDK can send two nulls. Nothing in its signature forbids it. toResult turns that case into a named failure instead of a crash. Keep the mapping at the SDK boundary. Code past this point gets PayResult and real non-null types.
If you own the Java code, also annotate it. Add @Nullable from androidx.annotation or JSpecify to both parameters. Then the compiler rejects the wrong override for every future caller.
The interview answer names the check, then the platform type
A live-coding round often hands you the crash log and the Kotlin override. Walk from the message to both causes.
- Read the message. It names the method and the parameter,
receiptId. - Say the null came from Java. Kotlin callers can’t pass null to a
Stringparameter. - Name the first cause. Kotlin generates a null check for each non-null parameter of a public function. It runs before the body, so the
errorguard never runs. - Name the second cause. The Java interface is unannotated, so
receiptIdis a platform type. The compiler acceptedStringwithout a warning. - Explain why tests passed. Only the decline path sends a null receipt.
- Give the fix. Make both parameters nullable and map them to a sealed result at the boundary. Annotate the Java side if you own it.
Step 5 tells the interviewer you would also add a failure-path test. Hass explains how that kind of reasoning is scored in finding the bug and still not passing. A library can reject a null body type too. See our post on the Retrofit 204 non-null crash.
Fixes that keep the declined card hidden
“Check receiptId for null first.” The compiler flags the condition as always true. Even if you keep it, the generated check has already thrown. Nothing inside the body runs on a null.
“Wrap charge() in try/catch.” On our synchronous stand-in this catches the NPE. You still lose card_declined, because the callback threw before reading it. If the SDK calls back on another thread, your catch isn’t on that stack at all.
“Compile with -Xno-param-assertions.” Our run with that flag printed failed: card_declined, so it looks fixed. The null is still there, only unchecked. We then stored receiptId in a String property and read its length later. That threw a plain NullPointerException at the read, far from the callback. The flag moves the crash somewhere harder to trace.
“Make only receiptId nullable.” It fixes this SDK’s decline path. The next unannotated callback will repeat the bug on a different parameter. Apply the nullable rule to every unannotated Java parameter you override.