{"id":1071,"date":"2026-11-25T13:44:00","date_gmt":"2026-11-25T12:44:00","guid":{"rendered":"https:\/\/grindloop.ai\/blog\/?p=1071"},"modified":"2026-11-24T19:39:26","modified_gmt":"2026-11-24T18:39:26","slug":"parameter-specified-as-non-null-is-null","status":"publish","type":"post","link":"https:\/\/grindloop.ai\/blog\/parameter-specified-as-non-null-is-null\/","title":{"rendered":"Parameter Specified as Non-Null Is Null: Why a Kotlin Override of a Java Callback Crashes Only on Errors"},"content":{"rendered":"<p>&#8220;Parameter specified as non-null is null&#8221; 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 <code>String<\/code> or <code>String?<\/code> without a warning. If you pick <code>String<\/code>, the compiler adds a null check at the top of the function. That check runs before your first line. So an <code>if (error != null)<\/code> 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.<\/p>\n\n<h2>Parameter specified as non-null is null only when the card is declined<\/h2>\n\n<p>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.<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ PaymentCallback.java\npackage sdk;\n\npublic interface PaymentCallback {\n    void onResult(String receiptId, Throwable error);\n}\n\n\/\/ PaymentClient.java (same package)\npublic class PaymentClient {\n    public void charge(int cents, PaymentCallback callback) {\n        if (cents &gt; 0) {\n            callback.onResult(\"rcpt_\" + cents, null);\n        } else {\n            callback.onResult(null, new IllegalArgumentException(\"card_declined\"));\n        }\n    }\n}<\/pre>\n\n\n<p>The Kotlin side looks careful. The developer knew <code>error<\/code> could be null, so it is <code>Throwable?<\/code>. They assumed a receipt always arrives, so <code>receiptId<\/code> is <code>String<\/code>. The body even checks the error first.<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class CheckoutViewModel {\n    var status = \"idle\"\n\n    fun pay(cents: Int) {\n        PaymentClient().charge(cents, object : PaymentCallback {\n            override fun onResult(receiptId: String, error: Throwable?) {\n                if (error != null) {\n                    status = \"failed: ${error.message}\"\n                    return\n                }\n                status = \"paid: $receiptId\"\n            }\n        })\n    }\n}<\/pre>\n\n\n<p>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.<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">paid: rcpt_1999\njava.lang.NullPointerException: Parameter specified as non-null is null:\n  method CheckoutViewModel$pay$1.onResult, parameter receiptId<\/pre>\n\n\n<p>The success path works, so a happy-path test passes. The decline crashes. The <code>card_declined<\/code> message never reaches the screen. On Android, an uncaught exception like this one crashes the app. Two separate rules stack to produce it.<\/p>\n\n<h2>A platform type lets you declare the parameter non-null<\/h2>\n\n<p><code>PaymentCallback<\/code> has no nullability annotations. Kotlin can&#8217;t know whether <code>receiptId<\/code> may be null. The <a href=\"https:\/\/kotlinlang.org\/docs\/java-interop.html\" target=\"_blank\" rel=\"noopener\">Java interop page<\/a> calls such types platform types. Kotlin relaxes null-safety checks for them. You choose the type. The compiler trusts your choice.<\/p>\n\n<p>So both <code>receiptId: String<\/code> and <code>receiptId: String?<\/code> compile as valid overrides. Neither gets a warning, because the compiler has no information to warn with. Only the SDK&#8217;s behavior says that a decline sends a null receipt.<\/p>\n\n<p>Annotations change this. The same page lists the annotation packages Kotlin reads, including <code>androidx.annotation<\/code>, JetBrains annotations and JSpecify. Since <a href=\"https:\/\/kotlinlang.org\/docs\/whatsnew21.html\" target=\"_blank\" rel=\"noopener\">Kotlin 2.1.0, released in November 2024<\/a>, a JSpecify nullability mismatch is an error by default. We added JSpecify&#8217;s <code>@Nullable<\/code> to both callback parameters and recompiled. The <code>receiptId: String<\/code> override failed with &#8220;&#8216;onResult&#8217; overrides nothing.&#8221; The compiler suggested <code>onResult(p0: String?, p1: Throwable?)<\/code> instead. That is why this crash mostly comes from older or unannotated Java SDKs.<\/p>\n\n<h2>Kotlin checks non-null parameters before the body runs<\/h2>\n\n<p>Java can pass null into any parameter. The <a href=\"https:\/\/kotlinlang.org\/docs\/java-to-kotlin-interop.html\" target=\"_blank\" rel=\"noopener\">Kotlin docs on calling Kotlin from Java<\/a> 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 <code>onResult<\/code> gets a check on <code>receiptId<\/code> at its very first instruction.<\/p>\n\n<p>The developer&#8217;s guard reads <code>error<\/code>. That parameter is nullable, so it has no generated check. But the check on <code>receiptId<\/code> runs first. On the decline path it sees null and throws. The guard and the failure branch never execute.<\/p>\n\n<p>The exception type has changed over time. Since <a href=\"https:\/\/kotlinlang.org\/docs\/whatsnew14.html\" target=\"_blank\" rel=\"noopener\">Kotlin 1.4, released in August 2020<\/a>, these checks throw <code>java.lang.NullPointerException<\/code>. Earlier versions threw <code>IllegalArgumentException<\/code> 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.<\/p>\n\n<h2>Declare every unannotated callback parameter nullable, then map the nulls<\/h2>\n\n<p>Make both parameters nullable. Then turn the pair into one result type. The rest of the code never sees two nullable values.<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sealed interface PayResult {\n    data class Paid(val receiptId: String) : PayResult\n    data class Failed(val cause: Throwable) : PayResult\n}\n\nfun toResult(receiptId: String?, error: Throwable?): PayResult = when {\n    error != null -&gt; PayResult.Failed(error)\n    receiptId != null -&gt; PayResult.Paid(receiptId)\n    else -&gt; PayResult.Failed(IllegalStateException(\"SDK returned neither receipt nor error\"))\n}\n\nclass CheckoutViewModel {\n    var status = \"idle\"\n\n    fun pay(cents: Int) {\n        PaymentClient().charge(cents, object : PaymentCallback {\n            override fun onResult(receiptId: String?, error: Throwable?) {\n                status = when (val r = toResult(receiptId, error)) {\n                    is PayResult.Paid -&gt; \"paid: ${r.receiptId}\"\n                    is PayResult.Failed -&gt; \"failed: ${r.cause.message}\"\n                }\n            }\n        })\n    }\n}<\/pre>\n\n\n<p>The same run now prints both outcomes.<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">paid: rcpt_1999\nfailed: card_declined<\/pre>\n\n\n<p>The <code>else<\/code> branch covers a case the signature allows. A Java SDK can send two nulls. Nothing in its signature forbids it. <code>toResult<\/code> turns that case into a named failure instead of a crash. Keep the mapping at the SDK boundary. Code past this point gets <code>PayResult<\/code> and real non-null types.<\/p>\n\n<p>If you own the Java code, also annotate it. Add <code>@Nullable<\/code> from <code>androidx.annotation<\/code> or JSpecify to both parameters. Then the compiler rejects the wrong override for every future caller.<\/p>\n\n<h2>The interview answer names the check, then the platform type<\/h2>\n\n<p>A live-coding round often hands you the crash log and the Kotlin override. Walk from the message to both causes.<\/p>\n\n<ol>\n<li>Read the message. It names the method and the parameter, <code>receiptId<\/code>.<\/li>\n<li>Say the null came from Java. Kotlin callers can&#8217;t pass null to a <code>String<\/code> parameter.<\/li>\n<li>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 <code>error<\/code> guard never runs.<\/li>\n<li>Name the second cause. The Java interface is unannotated, so <code>receiptId<\/code> is a platform type. The compiler accepted <code>String<\/code> without a warning.<\/li>\n<li>Explain why tests passed. Only the decline path sends a null receipt.<\/li>\n<li>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.<\/li>\n<\/ol>\n\n<p>Step 5 tells the interviewer you would also add a failure-path test. Hass explains how that kind of reasoning is scored in <a href=\"https:\/\/grindloop.ai\/blog\/android-debugging-interview-second-bug\/\">finding the bug and still not passing<\/a>. A library can reject a null body type too. See our post on the <a href=\"https:\/\/grindloop.ai\/blog\/retrofit-204-null-body-crash\/\">Retrofit 204 non-null crash<\/a>.<\/p>\n\n<h2>Fixes that keep the declined card hidden<\/h2>\n\n<p>&#8220;Check <code>receiptId<\/code> for null first.&#8221; 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.<\/p>\n\n<p>&#8220;Wrap <code>charge()<\/code> in try\/catch.&#8221; On our synchronous stand-in this catches the NPE. You still lose <code>card_declined<\/code>, because the callback threw before reading it. If the SDK calls back on another thread, your catch isn&#8217;t on that stack at all.<\/p>\n\n<p>&#8220;Compile with <code>-Xno-param-assertions<\/code>.&#8221; Our run with that flag printed <code>failed: card_declined<\/code>, so it looks fixed. The null is still there, only unchecked. We then stored <code>receiptId<\/code> in a <code>String<\/code> 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.<\/p>\n\n<p>&#8220;Make only <code>receiptId<\/code> nullable.&#8221; It fixes this SDK&#8217;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.<\/p>","protected":false},"excerpt":{"rendered":"<p>Parameter specified as non-null is null? A Java callback passed null into a non-null Kotlin override. Runnable repro, the nullable fix and the interview answer.<\/p>\n","protected":false},"author":2,"featured_media":1072,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"Parameter Specified as Non-Null Is Null: Why a Kotlin Override of a Java Callback Crashes Only on Errors","rank_math_description":"Parameter specified as non-null is null? A Java callback passed null into a non-null Kotlin override. Runnable repro, the nullable fix and the interview answer.","rank_math_focus_keyword":"parameter specified as non-null is null","footnotes":""},"categories":[9,7],"tags":[15,16,121,12,98,45],"class_list":["post-1071","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bug-squash","category-java","tag-android","tag-interview-prep","tag-java-interop","tag-kotlin","tag-null-safety","tag-technical-interview"],"_links":{"self":[{"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/posts\/1071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/comments?post=1071"}],"version-history":[{"count":1,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/posts\/1071\/revisions"}],"predecessor-version":[{"id":1073,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/posts\/1071\/revisions\/1073"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/media\/1072"}],"wp:attachment":[{"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/media?parent=1071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/categories?post=1071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/tags?post=1071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}