title: How do I call a Java method that takes a Non-null Void parameter from Kotlin?
body: <p><strong>Primary Question</strong></p>
  <p>Is it possible to call a Java method with a signature like this (from a third-party library, so the method signature cannot be changed) from Kotlin? If so, how?</p>
  <pre><code>class Uncallable {
      void myMethod(@NonNull Void a) {
          System.out.println(&quot;Ran myMethod&quot;);
      }
  }
  </code></pre>
  <p>I can call this from another Java class with</p>
  <pre><code>Uncallable u = new Uncallable();
  u.myMethod(null); // IDE warns about nullability but still runs fine
  </code></pre>
  <p>but from Kotlin, none of these options work</p>
  <pre><code>val u = Uncallable()
  u.myMethod() // No value passed for parameter 'a'
  u.myMethod(null) // Null can not be a value of a non-null type Void
  u.myMethod(Unit as Void) // ClassCastException: class kotlin.Unit cannot be cast to class java.lang.Void
  u.myMethod(Void)
  u.myMethod(Unit)
  u.myMethod(Nothing)
  </code></pre>
  <p>Similar questions (like <a href="https://stackoverflow.com/questions/58917088/pass-null-to-non-nullable-java-method-from-kotlin">this one</a>) suggest making a Java interface layer to let me get around the nullability rules. That works here, but I'd like to know if there's any way to call this without such an extra layer. I don't actually need to pass a null value, just to call the method.</p>
  <p><a href="https://stackoverflow.com/questions/33808076/using-java-void-type-in-kotlin">This question</a> addresses a similar issue, but there they have control of the interface and can just use <code>Void?</code> as the type.</p>
  <p><strong>Why do I want to do this?</strong></p>
  <p>I don't really... However, I have some unit tests that intercept the <code>addOnSuccessListener</code> callback added to some Firebase methods and call <code>onSuccess</code> on the provided listener. The signature of that callback uses <code>Void</code> for its parameter type, and previously I could call <code>listener.onSuccess(null)</code></p>
  <pre><code>doAnswer { invocation -&gt;
      val args = invocation.arguments
      @Suppress(&quot;UNCHECKED_CAST&quot;)
      val l = args[0] as OnSuccessListener&lt;Void&gt;
      l.onSuccess(null)
      mMockTask
  }.`when`(mMockTask).addOnSuccessListener(ArgumentMatchers.any())
  </code></pre>
  <p>After a recent update of Firebase libraries, it seems that either a <code>@NonNull</code> annotation was added to the parameter for <code>onSuccess</code>, or Kotlin changed to start enforcing it</p>
  <pre><code>public interface OnSuccessListener&lt;TResult&gt; {
      void onSuccess(@NonNull TResult var1);
  }
  </code></pre>
  <p>As a result, I can no longer call <code>listener.onSuccess(null)</code> - I get a build error about null being passed for a non-null parameter.</p>
  <p>I am able to make a <code>VoidCaller</code> Java interface to get around this,</p>
  <pre><code>public class VoidCaller {
      static void callSuccessCallback(OnSuccessListener&lt;Void&gt; callback) {
          callback.onSuccess(null);
      }
  
      static void callFailureCallback(OnFailureListener callback) {
          callback.onFailure(null);
      }
  }
  </code></pre>
  <p>which works, but only if I change my callbacks from</p>
  <pre><code>fileRef.delete().addOnSuccessListener { onSuccess() }
  </code></pre>
  <p>to</p>
  <pre><code>fileRef.delete().addOnSuccessListener { _ : Void? -&gt; onSuccess() }
  </code></pre>
  <p>or I get an error attempting to set <code>it</code> of a non-null type to null.</p>
  <pre><code>Parameter specified as non-null is null: method com.app.firebaseHelper.deleteImage$lambda-11$lambda-10, parameter it
  </code></pre>
  <p>Ideally I would like to find a way to call such a method directly from Kotlin.</p>