How to access a primitive array in C/C++?
I can not find a solution for this problem.
I know how to access primitive types with that Get<type>Field an Set<type>Field. But how can I access a primitive array in the same way?
I don't want to use a parameter reference. But it seems to be the only way.
Just to visualize it:
A Java Class:
class X
{
...
boolean[] b;
...
}
The jni side:
...
fieldID arrayid = env->GetFieldID(cls, "booleanArrayReturn", "[Z"); //this works: I get a non-null value
jbooleanArray TheBooleanArray = (jbooleanArray) env->GetBooleanField(obj, arrayid); //this doesn't work: I get a null
...
It should be something like GetBooleanArrayField, but this command doesn't exists.
Could anyone help me? I'm gradually going sick of that jni mess...
Thanks a lot.
Robert
[891 byte] By [
tailorman] at [2007-9-26 12:30:52]

Sorry, a little mistake:"booleanArrayReturn" should be "b"But unfortunatly this isn't the problem (I wish it would be)
The simple way - to use GetObjectField
jbooleanArray TheBooleanArray = env->GetObjectField(obj, arrayid);
jboolean *body = env->GetBooleanArrayElements(TheBooleanArray, 0);
//you code here...
env->ReleaseBooleanArrayElements(TheBooleanArray, body, 0);
Thanks.
Unfortunatly, this doesn't work yet. I've got a compiler error when GetOjectField is invoked. But a cast helps (jbooleanArray). Then there is no compiler error anymore.
Anyway, I found out, that it doesn't work, when the array reference in java is null. That means, that the array isn't initialized with something. But when I initialize it with something, it is accesible in jni.
However, that is not want I wanted. Since I'd like to initialize it with jni and without knowing before which size the array will have (dynamic). Who could I achieve this?
Robert
In Java.......
*********
class X {
private boolean init;
private int size;
private boolean b[];
public void isInit() {
if(!init)
System.out.println("Not initialized yet...");
else
System.out.println("Initialized successfully !");
}
void X() {
init=false;
}
public void type() {
if(init)
for(int i=0;i<size;i++)
System.out.println(b);
}
public static void main(String args[]) {
X temp=new X();
temp.type();
temp.isInit();
temp.makeInit(5);
temp.type();
temp.isInit();
}
static {
System.loadLibrary("x");
}
public native void makeInit(int size);
}
In C ................................
#include ><stdio.h>
#include <jni.h>
#include "X.h"
JNIEXPORT void JNICALL Java_X_makeInit(JNIEnv *env, jobject xObj, jint xSize) {
int i;
jclass X;
jboolean *bodyX;
jfieldID fid;
jbooleanArray arrX;
jsizelenX;
jboolean isCopy;
X = (*env)->GetObjectClass(env, xObj);
fid = (*env)->GetFieldID(env, X, "b", "[Z");
arrX=(*env)->NewBooleanArray(env, xSize);
lenX = (*env)->GetArrayLength(env, arrX);
bodyX = (*env)->GetBooleanArrayElements(env, arrX, &isCopy);
for(i=0;i<xSize;i++) {
bodyX=JNI_TRUE;
}
(*env)->SetObjectField(env, xObj, fid, arrX);
fid = (*env)->GetFieldID(env, X, "init", "Z");
(*env)->SetBooleanField(env, xObj, fid, JNI_TRUE);
fid = (*env)->GetFieldID(env, X, "size", "I");
(*env)->SetIntField(env, xObj, fid, lenX);
(*env)->ReleaseBooleanArrayElements(env, arrX, bodyX, 0);
return;
}
Result..............
***********************
Not initialized yet...
true
true
true
true
true
Initialized successfully !
> In Java.......
> *********
>
>
> class X {
> private boolean init;
> private int size;
> private boolean b[];
>
> public void isInit() {
> if(!init)
> System.out.println("Not initialized yet...");
> else
> System.out.println("Initialized successfully !");
> }
>
>
> void X() {
> init=false;
> }
>
> public void type() {
> if(init)
>for(int i=0;i<size;i++)
> System.out.println(b);
> }
>
> public static void main(String args[]) {
> X temp=new X();
> temp.type();
> temp.isInit();
>
> temp.makeInit(5);
> temp.type();
> temp.isInit();
>
> }
> static {
> System.loadLibrary("x");
> }
>
> public native void makeInit(int size);
> }
>
> In C ................................
>
> #include ><stdio.h>
> #include <jni.h>
> #include "X.h"
>
> JNIEXPORT void JNICALL Java_X_makeInit(JNIEnv *env,
> jobject xObj, jint xSize) {
>
> int i;
> jclass X;
> jboolean *bodyX;
> jfieldID fid;
> jbooleanArray arrX;
> jsizelenX;
> jboolean isCopy;
>
>
>
> X = (*env)->GetObjectClass(env, xObj);
>
> fid = (*env)->GetFieldID(env, X, "b", "[Z");
>
> arrX=(*env)->NewBooleanArray(env, xSize);
>
> lenX = (*env)->GetArrayLength(env, arrX);
>
> bodyX = (*env)->GetBooleanArrayElements(env, arrX,
> &isCopy);
>
> for(i=0;i<xSize;i++) {
> bodyX=JNI_TRUE;
> }
>
>
> (*env)->SetObjectField(env, xObj, fid, arrX);
>
>
> fid = (*env)->GetFieldID(env, X, "init", "Z");
>
>
> (*env)->SetBooleanField(env, xObj, fid, JNI_TRUE);
>
> fid = (*env)->GetFieldID(env, X, "size", "I");
>
> (*env)->SetIntField(env, xObj, fid, lenX);
>
> (*env)->ReleaseBooleanArrayElements(env, arrX, bodyX,
> 0);
>
> return;
>
> }
>
>
> Result..............
> ***********************
> Not initialized yet...
> true
> true
> true
> true
> true
> Initialized successfully !
>
>
>
>
>
>
so useful sample.....
but when i tried this method on Int..........is seems not success.......
any comment?
