What happens when I throw a C++ exception from a native Java method?
Within the JNI literature, the word exception appears to be used exclusively to refer to Java exceptions. Unexpected events in native code are referred to as programming errors. JNI explicitly does not require JVMs to check for programming errors. If a programming error occurs, behavior is undefined. Different JVMs may behave differently.
It's the native code's responsibility to translate all programming errors into either return codes or Java exceptions. Java exceptions don't get thrown immediately from native code. They can be pending, only thrown once the native code returns to the Java caller. The native code can check for pending exceptions with ExceptionOccurred
and clear them with ExceptionClear
.
Exception while calling java method from cpp using JNI
The initialization of MyManager
throws a NullPointerException
, which probably prevents determining the method id, so you might want to look at that. Note that not only the class initializer could throw this but also initialization of any static fields. You could put all initialization into the class initializer and then debug from there, something there must be null
.
JNI error while calling a C subroutine
You need to have your library explicitly set on your path.
It may be the case that the flags you are using there aren't quite right. Try this:
gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at \
-I[Java_HOME]/include -I[Java_HOME]/include/win32 \
-shared -o Sample1.dll Sample1.c
From MinGW GCC site.
An exception happened in constructor of class com.mgr.ui.viewmodels.MainViewModel
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void androidx.compose.runtime.MutableState.setValue(java.lang.Object)' on a null object reference
You can try moving your init {}
block to be after you declare displayMenu
. I am not confident that this will work, but it is worth a try.
If that does not help, then you may need to postpone your use of the SavedStateHandle
until sometime when the MutableState
is ready for use.
ClassNotFoundException I didn't succeed to catch
You only catch exceptions you list in catch(...). The stack trace shows the exception in the main method is an NoClassDefFoundError.
To solve it replace
catch (OWLOntologyCreationException|OWLOntologyStorageException|ClassNotFoundException e) {
System.out.println(name + " has not been saved into RDF/XML;");
System.out.println(e.getMessage());
}
By (here you catch all exceptions)
catch (Exception e) {
System.out.println(name + " has not been saved into RDF/XML;");
System.out.println(e.getMessage());
}
Or by
catch (OWLOntologyCreationException|OWLOntologyStorageException
|ClassNotFoundException|NoClassDefFoundErrore) {
System.out.println(name + " has not been saved into RDF/XML;");
System.out.println(e.getMessage());
}
Related Topics
How to Use Vectordrawable in Buttons and Textviews Using Android:Drawableright
Garbage Collector Log (Loggc) File Rotation with Logrotate Does Not Work Properly
Hibernate: Different Object with the Same Identifier Value Was Already Associated with the Session
Get Domain Name from Given Url
Content Is Not Allowed in Prolog Saxparserexception
Difference Between 'Optional.Orelse()' and 'Optional.Orelseget()'
Can a Java Class Add a Method to Itself at Runtime
Run Jar File in Command Prompt
How to Include Raw JSON in an Object Using Jackson
How to Set Android Lock Screen Image
How to Get the Current Screen Orientation
Why Does the Jvm Consume Less Memory Than -Xms Specified
Expected Begin_Array But Was Begin_Object at Line 1 Column 2
Is There a Common Java Utility to Break a List into Batches
Jersey /* Servlet Mapping Causes 404 Error for Static Resources
Java 11 Package Javax.Xml.Bind Does Not Exist
Java Stack Overflow Error - How to Increase the Stack Size in Eclipse