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;
}
================================================== ==============================