NullPointerException when Calling getFilesDir() in a JNI Call


I'm trying to call Java's getFilesDir() from a C library through a JNI call and I'm hitting a NullPointerException that I don't understand. Here's the exception:

W/System.err( 1576): java.lang.NullPointerException W/System.err( 1576): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109) W/System.err( 1576): at MYService.getFilesDirPath(MYService.java:1150) W/System.err( 1576): at MYService.UtvGetPeristentPath(Native Method)

The problem is happening in this function:

public String getFilesDirPath() { try { return getFilesDir().getPath(); // <--- Exception happens here! } catch(Exception e) { e.printStackTrace(); Log.e("DEBUG", "getFilesDirPath set exception", e); return null; } }

The JNI call is set up with a few static variables that are set by JNI_OnLoad:

static JavaVM *s_jvm = NULL; static jclass cls; static jmethodID mid; static jobject obj; jint JNI_OnLoad(JavaVM *vm, void *reserved) { s_jvm = vm; JNIEnv *env; if (s_jvm){ (*s_jvm)->AttachCurrentThread (s_jvm, &env, NULL); } jclass localRef_class; jmethodID localRef_mid; jmethodID constr; localRef_class = (*env)->FindClass(env, "MYService"); cls = (*env)->NewGlobalRef(env,localRef_class); constr = (*env)->GetMethodID(env, cls, "<init>", "()V"); obj = (*env)->NewGlobalRef(env, (*env)->NewObject(env, cls, constr)); return JNI_VERSION_1_6; }

And here is the JNI function itself:

char *getFilesDirPath() { JNIEnv *jenv = NULL; __android_log_print(ANDROID_LOG_ERROR, "DEBUG", "JNI_Interface: getFilesDirPath"); if(s_jvm == NULL) return NULL; int check = (*s_jvm)->GetEnv(s_jvm,(void **)&jenv,JNI_VERSION_1_6); if (check != JNI_OK) { (*s_jvm)->AttachCurrentThread(s_jvm, &jenv, NULL); (*s_jvm)->GetEnv(s_jvm,(void **)&jenv,JNI_VERSION_1_6); } if(jenv != NULL) { // get the method mid = (*jenv)->GetMethodID(jenv, cls, "getFilesDirPath", "()Ljava/lang/String;"); if (mid == 0) { __android_log_print(ANDROID_LOG_ERROR, "DEBUG", "getFilesDirPath not found"); if (check != JNI_OK) (*s_jvm)->DetachCurrentThread (s_jvm); return NULL; } else{ jstring result = (jstring)(*jenv)->CallObjectMethod(jenv, obj, mid); char *filesDir = (char *) (*jenv)->GetStringUTFChars(jenv, result, 0); if (check != JNI_OK) (*s_jvm)->DetachCurrentThread (s_jvm); return filesDir; } } if (check != JNI_OK) (*s_jvm)->DetachCurrentThread (s_jvm); return NULL; }

The problem is definitely tied to getFilesDir(). If I avoid calling that function and pass back a debug string, everything seems to work. I think the problem is related to not properly setting up the Context, but I'm not sure what might be wrong.


Your MYService class probably extends an Android Service or Activity. These classes are usually not created with their constructor call. They are started by the system using Intents. You can usually use their full 'Context' functionality for the first time in their onCreate methods. So you should get the reference to your MYService from somewhere else - not by instanciating it from JNI.


  • Android libGDX error on launch No JNI_Onload found
  • NDK how to get the dvm point?
  • libgdx my first triangle tutorial not working?
  • java.lang.ExceptionInInitializerError in javacv
  • JNI: Can not get array length
  • Calling main method from JNI fails
  • Hibernate: Parent and Child relationship data structure
  • java.io.FileNotFoundException when image file is inside jar using jxl.write.WritableImage
  • How to change the output file name from part-00000 in reducer to inputfile name
  • Property file not reflecting the modified changes using Apache Commons Configuration
  • Know the offset of a file using BufferedReader?
  • Get path of data directory(android)
  • FileLocator's method toFileURL ignores empty folders
  • getResource() unable to read contents of a directory inside jar
  • android downloading multiple files with InputStream FileOutputStream
  • java.nio.file.FileSystemNotFoundException when getting file from resources folder
  • Help refreshing list with notifyDataSetChanged()
  • Put database into android project
  • How to extract text from Word files using C#?
  • javascript inside java/jsp code
  • sending/ receiving email in Java
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Android Studio and gradle
  • Proper way to use connect-multiparty with express.js?
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Checking variable from a different class in C#
  • Does armcc optimizes non-volatile variables with -O0?
  • reshape alternating columns in less time and using less memory
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal