473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

JNI CallVoidMethod (returning a jobject)

Wes
Hello,

I have a C++/JNI program that creates a Java object, populates the
members and returns the jobject to the calling Java program.
The code compiles and runs cleanly with no exceptions being raised.
However, I've noticed that the object values are not being set
correctly.
i.e. When the object is returned, it does not contain any values.
I'm using GetMethodID and CallVoidMethod to set the individual fields.

Any help would be much appreciated!


The object being returned is as follows:

================================================== ==============================
public class ReturnData {

private Float amount;
public EstimateData() {

super();

}
public Float getAmount() {
return amount;
}

public void setAmount(Float amount) {
this.amount = amount;
}

}
================================================== =================================
Native Code is as follows:
================================================== =================================
extern "C" JNIEXPORT jobject JNICALL Java_com_returnData
(JNIEnv * env, jobject thisObject, jstring string1)
{
// Find class to instantiate]

jclass cls = env->FindClass("com/ReturnData");
if (env->ExceptionOccurred())
{
env->ExceptionDescribe();
return NULL;
}

if (cls == 0)
printf ("Error2!");
// env->ThrowNew(jExceptionClass ,"\n Unable to find
ReturnData class");
return NULL;
}

jmethodID mID = env->GetMethodID(cls, "<init>", "()V");
if (env->ExceptionOccurred())
{
env->ExceptionDescribe();
return NULL;
}
// Creat new object
jobject jInstance = env->NewObject(cls, mID);
if (env->ExceptionOccurred())
{
env->ExceptionDescribe();
return NULL;
}

jmethodID methID = env->GetMethodID(cls, "setAmount",
"(Ljava/lang/Float;)V");

if (env->ExceptionOccurred())
{
env->ExceptionDescribe();
return NULL;
}

jfloat var1 = 11;
env->CallVoidMethod(jInstance,methID,var1);

// No exception raised here although the above line does not seem to
work?

if (env->ExceptionOccurred())
{
env->ExceptionDescribe();
return NULL;
}

methID = env->GetMethodID(cls, "getAmount",
"()Ljava/lang/Float;");

if (env->ExceptionOccurred())
{
env->ExceptionDescribe();
return NULL;
}

jfloat var2 = env->CallFloatMethod(jInstance,methID);
cout << var2;

// No exception raised. Expected '11' to be output to the console.
Program prints out "0" (from native code)
// From Java code, the 'amount' value appears as NULL. ..

if (env->ExceptionOccurred())
{
env->ExceptionDescribe();
return NULL;
}
return jInstance;

}
================================================== ==============================
Jul 17 '05 #1
0 9741

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
4
by: Tim Wong | last post by:
I have a java method setup where it will be accessed in C++ via JNI. The process is as follows: 1) C++ function getObject(...) is called. 2) getObject(...) uses JNI to access a java class...
8
by: Derek | last post by:
Some authors advocate returning const objects: const Point operator+(const Point&, const Point&); ^^^^^ Returning a const object prevents some bad code from compiling: Point a, b, c; (a +...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
7
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
6
by: EvilOldGit | last post by:
const Thing &operator++(int) { Thing temp = *this; operator++(); return temp; } Is this code robust ? I get a compiler warning about returning a reference to a a local, which I guess is...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.