Search code examples
androidjava-native-interface

Is it possible to do a setIntField() here ? an int in the place of Long in JNI


Is it possible to change this snippet of code so that SetLongField become setIntField ?

jobject nbField ;
UL l_ul_NbField ;

jclass longClass = i_env->GetObjectClass(nbField);
jfieldID val = i_env->GetFieldID(longClass, "value", "J");
i_env->SetLongField(nbField, val, (L)l_ul_NbField);

When I do :

jclass intClass = i_env->GetObjectClass(nbField);
jfieldID val = i_env->GetFieldID(intClass, "value", "I");
i_env->SetIntField(nbField, val, (I)l_ul_NbField);

It crashes ! Why ?


Solution

  • Can you make sure what type nbField is? If nbField has type Long, then i_env->GetFieldID(intClass, "value", "I"); returns null, so calling setIntField causes crash.