Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
manesh thomas
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic