> With regard to the clip above, do i need to do this if I am passing
> an (instantiated) object into the c code from the java code?
Yes, it's perfectly reasonable to use the enclosing object ("this")
when you create instances of its inner class.
> /* get the address field from MyClass */
> jfieldID fidStr = (*env)->GetFieldID(env, fid, "address",
> "Ljava/lang/String;");
>
> It crashes when I introduce the last line.
You're attempting to find the fieldID by looking it up in another
fieldID, but you should be looking in a class instead.
Start with the class HelloNative$MyClass, and from there you can look
up its fields:
jclass my_cls = (*env)->FindClass(env,"HelloNative$MyClass");
jfieldID addr_fid = (*env)->GetFieldID(env,my_cls,"address",
"Ljava/lang/String;");
Now to actually get or change the *value* associated with this field,
you need an *object* of that type (i.e. one that holds the field you
just looked up), and you choose one of the Get/Set<type>Field()
functions based on the type of the field itself (i.e. one of the
primitive types, or Object for all others).
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
stixwix - 06 Feb 2006 10:57 GMT
> Start with the class HelloNative$MyClass, and from there you can look
> up its fields:
[quoted text clipped - 8 lines]
> functions based on the type of the field itself (i.e. one of the
> primitive types, or Object for all others).
When I try the above with the following additional code - jstr is
NULL.:
jstring jstr = (*env)->GetObjectField(env, obj, addr_fid);
const char *str = (*env)->GetStringUTFChars(env, jstr, NULL);
All the examples I see use GetObjectClass instead of FindClass. I
can't find/understand any docs relating to FindClass, but it doesn't
imply object instance scope.
Gordon Beaton - 06 Feb 2006 11:38 GMT
> When I try the above with the following additional code - jstr is
> NULL.:
> jstring jstr = (*env)->GetObjectField(env, obj, addr_fid);
> const char *str = (*env)->GetStringUTFChars(env, jstr, NULL);
It's hard to tell from these code fragments, but is "obj" really an
instance of the class containing the field you are looking up?
I.e. you looked up up a field ID in HelloNative$MyClass, so "obj" must
be a HelloNative$MyClass object (or it won't have that field).
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e