aspose file tools*
The moose likes Java in General and the fly likes Problem while returning  an object using JNI and accessing its fields and methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem while returning  an object using JNI and accessing its fields and methods?" Watch "Problem while returning  an object using JNI and accessing its fields and methods?" New topic
Author

Problem while returning an object using JNI and accessing its fields and methods?

manesh thomas
Greenhorn

Joined: Feb 20, 2003
Posts: 1
I have to return an java object(IPDetails) from a java method using JNI.
I am using the following code for this.But the NULL value is getting.
Why this is becoming NULL.Is this the correct way of returning an Object
from a java function?
jobject ipDetails;
ipDetails = (*env)->CallObjectMethod(env,jobj,methodID,(*env)->NewStringUTF(env,userName));
The IPDetails class has 2 fields
a String field (ip) and a boolean field (accept)
.I want the 2 field values to be retrieved from the object
returned from the java method and be used in the C program.
For that,
jclass detailsClass = (*env)->FindClass(env,"com/test/IPDetails");
jfieldId ipID = (*env)->GetFieldID(env,detailsClass,"ip","Ljava/lang/String;");
jboolean acceptID = (*env)->GetFieldID(env,detailsClass,"accept","Z");
For getting the ip and accept fields of the ipDetails object which function
shall i use??
Can anyone suggest how can i call and get the above two fields
of the object returned.
Thanks
Vijayakumar Arya
Ranch Hand

Joined: Jan 27, 2003
Posts: 76
Hi Manesh,
I did not get one thing, where are you instantiating the IPDetails object? Whether the instance of IPDetails is passed as a parameter in the native method.
I guess that you are trying to access the fields of the IPDetails instance,
For accessing the field of any instance you have to use

Some examples:

you have to pass the field id returned by the GetFieldId function. Also you can refer to the JNI documentation available within the Java API Documentation.
Hope this helps
[ February 21, 2003: Message edited by: Vijayakumar Arya ]

Thanks,<p>Vijay<p>The Hand that gives, Gathers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem while returning an object using JNI and accessing its fields and methods?