# Invoke static java method with nullable long

**URL:** <https://discuss.kotlinlang.org/t/invoke-static-java-method-with-nullable-long/16894>\
**Category:** Uncategorized\
**Created:** [March 30, 2020, 2:46pm UTC](https://discuss.kotlinlang.org/t/invoke-static-java-method-with-nullable-long/16894 "2020-03-30T14:46:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [March 30, 2020, 2:46pm UTC](https://discuss.kotlinlang.org/t/invoke-static-java-method-with-nullable-long/16894/1 "2020-03-30T14:46:50Z")

</div>

There is a Java method in an external library that I want to invoke from Kotlin, and it accepts a nullable `long` parameter, but I don’t know how to pass null.

Java:

```auto
public static void glfwSetCursor(@NativeType("GLFWwindow *") long window, @NativeType("GLFWcursor *") long cursor) {

```

`cursor` is supposed to be nullable, but if I have in Kotlin:

`GLFW.glfwSetCursor(window, null)`  
I get a compilation error: “Null can not be a value of a non-null type Long”

How do I pass null here and have it compile?

---

<div class="post-metadata">

**Author:** ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)\
**Post date:** [March 30, 2020, 3:41pm UTC](https://discuss.kotlinlang.org/t/invoke-static-java-method-with-nullable-long/16894/2 "2020-03-30T15:41:56Z")

</div>

Would that do the job?

```java
public static void glfwSetCursor(@NativeType("GLFWwindow *") long window, @NativeType("GLFWcursor *") @Nullable Long cursor) {}

```

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 30, 2020, 3:47pm UTC](https://discuss.kotlinlang.org/t/invoke-static-java-method-with-nullable-long/16894/3 "2020-03-30T15:47:20Z")

</div>

glfw uses `long` to represent C pointers. So `null` would just be `0`.

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [March 30, 2020, 3:55pm UTC](https://discuss.kotlinlang.org/t/invoke-static-java-method-with-nullable-long/16894/4 "2020-03-30T15:55:27Z")

</div>

I don’t have control over the GLFW code

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [March 30, 2020, 3:57pm UTC](https://discuss.kotlinlang.org/t/invoke-static-java-method-with-nullable-long/16894/5 "2020-03-30T15:57:37Z")

</div>

Nice, that’s what I was doing, and yeah I think it was working but because the docs say to pass null, I had a TODO in there to check it out.

---

<div class="post-metadata">

**Author:** ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)\
**Post date:** [March 30, 2020, 4:10pm UTC](https://discuss.kotlinlang.org/t/invoke-static-java-method-with-nullable-long/16894/6 "2020-03-30T16:10:38Z")

</div>

If that’s so then there is no workaround it. 😉 Primitives in Java can’t have a `null` value.

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [March 30, 2020, 5:05pm UTC](https://discuss.kotlinlang.org/t/invoke-static-java-method-with-nullable-long/16894/7 "2020-03-30T17:05:48Z")

</div>

Ok, then it seems to be a mistake in the GLFW docs. Thanks.  
It’s been a while since I’ve done any plain Java 😉 I wasn’t sure if `long i` was synonymous for using its wrapper class `@Nullable Long i` or not.  
A quick scratchpad confirms you’re correct, even though you can do `Long i = null;` and pass i as a `long`, it’s a NRE.
