Calling a Java method with null argument

381 views
Skip to first unread message

Shyam Krishna

unread,
Nov 29, 2022, 10:45:03 AM11/29/22
to android-ndk
Hi,

I want to call a method with null in its argument. Trying to pass nullptr throws an error in the IDE saying: 'void *' is not a valid JVM type.

What is the appropriate way to do this?

To illustrate, the Java method looks something like this:

private void nativeCallback(byte[] data) {
if (data == null) {
//do something
} else {
//do something else
}
}
And the JNI call looks like this:
METHOD_nativeCallback = env->GetMethodID(cls, "nativeCallback", "([B)V");
env->CallVoidMethod(obj, METHOD_nativeCallback, nullptr);

Thanks,
-Shyam

enh

unread,
Nov 29, 2022, 11:09:14 AM11/29/22
to andro...@googlegroups.com, Jomo Fisher
that seems like it might be a Studio bug? +Jomo Fisher 

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/11250ffa-7ebf-4a8f-912e-730e88757467n%40googlegroups.com.

Shyam Krishna

unread,
Nov 29, 2022, 11:44:24 AM11/29/22
to android-ndk
OK, to add some more clarity, the error doesn't stop the project from building or otherwise functioning, but within the editor it shows up as an error with the code inspection message I mentioned above.

-Shyam

enh

unread,
Nov 29, 2022, 11:53:16 AM11/29/22
to andro...@googlegroups.com, Jomo Fisher
yeah, that code looks right to me (one of the authors of ART's JNI implementation, though it is a bit early in the morning for my brain to be fully functional :-) ), which is why i think Studio is just confused. maybe because it's a varargs function?

if this was C and you'd said `NULL`, that would be different --- then you might be looking at an integer 0 and now you have to ask questions about what that gets promoted to for the varargs call. but since this is C++ and you've said nullptr, i think that's unambiguously a pointer.

but that would be a _compiler_ warning anyway, and this is a Studio-specific warning. i think their model for types going into JNI functions is just incomplete. +Jomo Fisher again, since he seems to have been dropped by the list.

Shyam Krishna

unread,
Dec 2, 2022, 10:27:46 AM12/2/22
to android-ndk
There doesn't seem to be a definite answer yet so I'll just share my final observations:

I tested passing nullptr from the JNI code and the code compiled and ran, and the Java method ended up receiving null. I believe that this should be the way to do it and the error Android Studio throws is a bug.

-Shyam

enh

unread,
Dec 2, 2022, 10:29:52 AM12/2/22
to andro...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages