indira
indira

Reputation: 6687

in JNI, how to assign values to jobjectArray?

I have an array of objects which is initialized in Java as shown below:

Record[] pRecords = new Record[5];
ret = GetRecord(pRecords);

I passed this array to the JNI, from JNI it will call CPP, and finally the array will get filled.

JNIEXPORT jint JNICALL Java_GetRecord
  (JNIEnv *jEnv, jobject ObjApp, jobjectArray jRecords)
{
    Record  *pRecords = (Record *)malloc(5*sizeof(Record  ));   

    ret = Get_Record(pRecords); // call to CPP
    if(SUCCESS == ret)
    {
        jclass c = (jEnv)->GetObjectClass(jRecords);
        jfieldID  fid = (jEnv)->GetFieldID(c, "m_session", "I");
        (jEnv)->SetIntField (jRecords, fid, pRecords [0].value);
    }
}

I am getting fid NULL. How to assign pRecords[0].value to 0th array of jRecords ?

Upvotes: 3

Views: 4386

Answers (1)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

A jobjectArray is not a pointer to the first element of the array. Remember that Java arrays are themselves first-class objects.

fid is 0 because you're looking for a member m_session in the class that represents an array of the Java class Record; of course the array class has no such member. You need to do a FindClass() to get the Record class, and then look for the member in there.

Then you proceed to try to set that member. If it's actually a member of the Record class, I'd imagine you want to set the value of that member in each array element in a loop, yes? Certainly not in the array itself, as you're trying to do. For each array element you need to call the appropriate method to get the object in that array position, then operate on that object.

Upvotes: 2

Related Questions