Is it possible to track down which expression caused an NPE?
When an exception happens, JVM knows the original bytecode that caused the exception. However, StackTraceElement
does not track bytecode indices.
The solution is to capture bytecode index using JVMTI whenever exception occurs.
The following sample JVMTI agent will intercept all exceptions, and if exception type is NullPointerException
, the agent will replace its detailMessage
with the bytecode location information.
#include <jvmti.h>
#include <stdio.h>
static jclass NullPointerException;
static jfieldID detailMessage;
void JNICALL VMInit(jvmtiEnv* jvmti, JNIEnv* env, jthread thread) {
jclass localNPE = env->FindClass("java/lang/NullPointerException");
NullPointerException = (jclass) env->NewGlobalRef(localNPE);
jclass Throwable = env->FindClass("java/lang/Throwable");
detailMessage = env->GetFieldID(Throwable, "detailMessage", "Ljava/lang/String;");
}
void JNICALL ExceptionCallback(jvmtiEnv* jvmti, JNIEnv* env, jthread thread,
jmethodID method, jlocation location, jobject exception,
jmethodID catch_method, jlocation catch_location) {
if (env->IsInstanceOf(exception, NullPointerException)) {
char buf[32];
sprintf(buf, "location=%ld", (long)location);
env->SetObjectField(exception, detailMessage, env->NewStringUTF(buf));
}
}
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
jvmtiEnv* jvmti;
vm->GetEnv((void**)&jvmti, JVMTI_VERSION_1_0);
jvmtiCapabilities capabilities = {0};
capabilities.can_generate_exception_events = 1;
jvmti->AddCapabilities(&capabilities);
jvmtiEventCallbacks callbacks = {0};
callbacks.VMInit = VMInit;
callbacks.Exception = ExceptionCallback;
jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);
jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL);
return 0;
}
Compile this into a shared library and run java with -agentpath
option:
java -agentpath:/pato/to/libRichNPE.so Main
JEP 358: Helpful NullPointerExceptions adds such a feature to OpenJDK 14. It is disabled by default; you have to specify -XX:+ShowCodeDetailsInExceptionMessages
to enable it. With it, your example results in:
Exception in thread "main"
java.lang.NullPointerException: Cannot invoke "String.isEmpty()" because "st" is null
at TestNPE.main(TestNPE.java:7)
Classes do not need to be recompiled to take advantage of this feature. It was originally developed for the SAP JVM.
The exception itself does not have enough information to provide more than line numbers.
One option i see is to use a bytecode debugger like bytecode visualizer to closer localize the bytecode instruction that causes the npe. Step forward until the exception occurs, or add a breakpoint for npe.