Get int value from java

Viewed 36

im trying to get the value of an int which is non static

java code

public int valuex = 500;
public native int intfromjni();

native code

JNIEXPORT jint JNICALL Java_com_samplejni_cls_intfromjni(JNIEnv* env, jobject thiz)
{
     jclass x = env->GetObjectClass(thiz);
     jfieldID f = env->GetFieldID(x, "valuex", "I");
     int i = env->GetIntField(x, f);
     return i;
}

but it throws crash with log

JNI DETECTED ERROR IN APPLICATION: jfieldID int com.samplejni.cls.valuex not valid for an object of class java.lang.Class<com.samplejni.cls>
1 Answers

here

int i = env->GetIntField(x, f);

you trying to use jclass to get a particular object's value. It would work for static fields.

For non-static firelds you should use jobject:

int i = env->GetIntField(thiz, f);
Related