Search code examples
javacjava-native-interface

NullPointerException trying to populate array from JNI


I receive a null pointer exception when I call setTimeStamp method from JNI.I want to populate the array on JNI side .(Array is Array of custom objects)

DataAvailable

public class DataAvailable {
String timestamp;
public void setTimeStamp(String timestamp) {
    System.out.println("Timestamp is "+timestamp);
    this.timestamp = timestamp;
}

JNIWrapper.java

public native int pax_store_get_data_avail_info(DataAvailable[] stats_array);

JNI Code

JNIEXPORT jint JNICALL Java_demo_JNIWrapper_pax_1store_1get_1data_1avail_1info
  (JNIEnv *env, jclass jclass1, jobjectArray jobj){

jclass dataClass=(*env)->FindClass(env,"demo/DataAvailable");//Get the java class 

for(i=0;i< 2;i++)
    {
                jobject j=(*env)->GetObjectArrayElement(env,jobj,i);
jmethodID meth=(*env)->GetMethodID(env,dataClass,"setTimeStamp","(Ljava/lang/String;)V");//this gets the method id present in the class 


               if(meth==NULL){
                printf("Method set is null");
                }

jstring x =  (*env)->NewStringUTF(env,"2013-12");
        (*env)->CallObjectMethod(env,j,meth,x);//This is where I get Null Pointer

}

 (*env)->DeleteLocalRef(env, dataClass);

return 0;

}

Solution

  • As Suggested by @Alex Cohn and @immbis ,the array passed into the JNI was not filled hence was getting a null pointer

    Thank you Alex Cohn and immbis for your suggestions .