Invoke static java method with nullable long

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.


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?

Would that do the job?

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

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

1 Like

I don’t have control over the GLFW code

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.

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

1 Like

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 :wink: 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.