JNI- Problem with setObjectArrayElement


Joined
Dec 11, 2008
Messages
1
Reaction score
0
Hello

I am trying to pass an object array to my JNI function and i want My JNI Function to Copy all these objects in a new object array and return it.

Following is my code

ObjectArray.java

class ObjectArray {
public native String[] setValues(String[] platforms);
//public native MyClass[] getMyArray();
public static void main(String[] args) {

ObjectArray objArr = new ObjectArray();
String[] platforms = new String[3];
String[] platforms_frm_C = new String[3];
platforms[0] = new String("Sun Solaris");
platforms[1] = new String("HP-UX");
platforms[2] = new String("RedHat Linux");


for(int i=0;i<platforms.length;i++) {
System.out.println("Platform "+i+": "+platforms);
}

platforms_frm_C = objArr.setValues(platforms);
/*for(int j=0;j<platforms_frm_C.length;j++) {
System.out.println("Platform "+j+": "+platforms_frm_C[j].toString());
}*/

}
static {
System.loadLibrary("ObjectArray");
}
}


And following is my native code in C++

#include <jni.h>

#include <stdio.h>

#include "ObjectArray.h"



JNIEXPORT jobjectArray JNICALL
Java_ObjectArray_setValues(JNIEnv *env,
jobject obj,
jobjectArray oarr)
{



jsize size = (*env)->GetArrayLength(env, oarr);

jobjectArray result;

char const* argv[128];
int i;
jclass intArrCls = (*env)->FindClass(env, "[Ljava/lang/String;");
if (intArrCls == NULL) {
return NULL; /* exception thrown */
}



result = (*env)->NewObjectArray(env, size, intArrCls, NULL);
if (result == NULL) {
return NULL; /* out of memory error thrown */
}
printf ("size = %d\n",size);




for (i = 0; i < size; i++)
{
/* obtain the current object from the object array */
jobject myObject = (*env)->GetObjectArrayElement(env, oarr, i);

/* Convert the object just obtained into a String */
const char *str = (*env)->GetStringUTFChars(env,myObject,0);

/*if("HP-UX" != str) {
printf ("STR = %s\n",str);
str = "WINDOWS";
} */

/* Build the argv array */
argv = str;
printf ("argv[%i] = %s\n",i,argv);


/* Free up memory to prevent memory leaks */
(*env)->ReleaseStringUTFChars(env, myObject, str);

(*env)->SetObjectArrayElement(env, result, i, myObject);
}
return result;

}


When i try to execute ObjectArray.java, i get follwing Error

Exception in thread "main" java.lang.ArrayStoreException
at ObjectArray.setValues(Native Method)
at ObjectArray.main(ObjectArray.java:18)

Line No 18 of ObjectArray.java -- > platforms_frm_C = objArr.setValues(platforms);

Any Help would be Appriciated !

Thanks
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top