When trying your code you could easily add some assertions like this:
JNIEXPORT void JNICALL Java_accessBuffThroughObjectTwo(JNIEnv *env, jobject obj, jobject objectTwo) {
jclass clazz;
jclass bufferClazz;
jobject bufferJObject;
jfieldID fid;
clazz = (*env)->GetObjectClass(env, objectTwo);
assert(clazz != NULL);
fid = (*env)->GetFieldID(env, clazz, "obj", "Ljava/lang/Object;");
assert(fid != NULL);
bufferJObject = (*env)->GetObjectField(env, javascsicommand, fid);
assert(bufferJObject != NULL);
bufferClazz = (*env)->GetObjectClass(env, bufferJObject);
assert(bufferClazz != NULL);
fid = (*env)->GetFieldID(env, bufferClazz, "buff", "[B");
assert(fid != NULL);
}
Doing so you will first see that the first fid
will be NULL. That is because the ObjectTwo
class does not have any fields of type java.lang.Object
. You should change the line to look like this (but add the correct packages instead of com/package
):
fid = (*env)->GetFieldID(env, clazz, "obj", "Lcom/package/ObjectOne;");
If you run again you will find that the fid is no longer null and the assertion will pass.
As others have suggested I believe that the javascsicommand
should be objectTwo
.
Now the next place where the assertion will fail is on bufferJObject
. That is because the field exists but the object is NULL and if you check your java code you will notice that the obj
field is never instantiated and is null
.
Change your java code to something like this:
public class ObjectTwo
{
private ObjectOne obj = new ObjectOne();
...
...
}
You will now pass the assertion and even pass all other assertions.
To summarize you were accessing a null
object and trying to invoke reflection on it:
bufferClazz = (*env)->GetObjectClass(env, bufferJObject); <-- The bufferJObject was NULL
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…