Note that there are some explanatory texts on larger screens.

plurals
  1. POJNI - GetObjectField returns NULL
    text
    copied!<p>I'm currently working on <a href="http://mangler.org" rel="nofollow noreferrer">Mangler's</a> Android implementation.</p> <p>I have a java class that looks like so:</p> <pre><code>public class VentriloEventData { public short type; public class _pcm { public int length; public short send_type; public int rate; public byte channels; }; _pcm pcm; } </code></pre> <p>The signature for my pcm object:</p> <pre><code>$ javap -s -p VentriloEventData ... org.mangler.VentriloEventData$_pcm pcm; Signature: Lorg/mangler/VentriloEventData$_pcm; </code></pre> <p>I am implementing a native JNI function called getevent, which will write to the fields in an instance of the VentriloEventData class. For what it's worth, it's defined and called in Java like so:</p> <pre><code>public static native int getevent(VentriloEventData data); VentriloEventData data = new VentriloEventData(); getevent(data); </code></pre> <p>And my JNI implementation of getevent:</p> <pre><code>JNIEXPORT jint JNICALL Java_org_mangler_VentriloInterface_getevent(JNIEnv* env, jobject obj, jobject eventdata) { v3_event *ev = v3_get_event(V3_BLOCK); if(ev != NULL) { jclass event_class = (*env)-&gt;GetObjectClass(env, eventdata); // Event type. jfieldID type_field = (*env)-&gt;GetFieldID(env, event_class, "type", "S"); (*env)-&gt;SetShortField( env, eventdata, type_field, 1234 ); // Get PCM class. jfieldID pcm_field = (*env)-&gt;GetFieldID(env, event_class, "pcm", "Lorg/mangler/VentriloEventData$_pcm;"); jobject pcm = (*env)-&gt;GetObjectField( env, eventdata, pcm_field ); jclass pcm_class = (*env)-&gt;GetObjectClass(env, pcm); // Set PCM fields. jfieldID pcm_length_field = (*env)-&gt;GetFieldID(env, pcm_class, "length", "I"); (*env)-&gt;SetIntField( env, pcm, pcm_length_field, 1337 ); free(ev); } return 0; } </code></pre> <p>The code above works fine for writing into the type field (that is not wrapped by the _pcm class). Once getevent is called, data.type is verified to be 1234 at the java side :)</p> <p>My problem is that the assertion "pcm != NULL" will fail. Note that pcm_field != NULL, which probably indicates that the signature to that field is correct... so there must be something wrong with my call to GetObjectField. It looks fine though if I compare it to the <a href="http://java.sun.com/docs/books/jni/html/fldmeth.html" rel="nofollow noreferrer">official JNI docs</a>.</p> <p>Been bashing my head on this problem for the past 2 hours and I'm getting a little desperate.. hoping a different perspective will help me out on this one. </p> <p>edit1: This won't work with any nested object, even a simple 'Ljava/lang/String;' will fail.<br /> edit2: Both ExceptionOccurred and ExceptionCheck return JNI_FALSE.<br /> edit3: Won't work with static fields either.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload