JNI CallVoidMethod (returning a jobject)

From: Wes (wesleyknox_at_hotmail.com)
Date: 02/25/04


Date: 25 Feb 2004 06:27:46 -0800

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

================================================================================



Relevant Pages

  • Coding Help
    ... I am in an Into to Java class and this is my last week. ... classes in HTML coding yet, I have had one class in QBasic and am ... and three buttons ("Deposit" to add the amount to the ... balance, "Withdraw" to subtract the amount from the balance, and "Balance" ...
    (comp.lang.java.help)
  • Re: trouble passing float array from C to java method
    ... The application itself is in C, and uses JNI to invoke Java ... doing so will create a JNIEnv which is valid in, and only in, that OS thread. ... Any jobjects (local references) which are created via that JNIEnv are also ... jobject created during that call will be released when the ...
    (comp.lang.java.programmer)
  • Re: directory size
    ... >the logical contents of the file not the amount of disk space ... >reality the amount of disk sace taken is vastly different. ... I don't think there is any way in pure Java you can get the overhead ...
    (comp.lang.java.help)
  • how fast is java RPC ( such as remote tea)?
    ... I have a project that needs to send a large amount of data from c/c++ ... to java. ... The data is used to draw real-time map data (say with ...
    (comp.lang.java.programmer)
  • Challenge: Marshalling a jobjectArray from JNI
    ... I have got a DLL file which has been badly affected to be accessed from a ... jobjectArray (it is an array of java strings). ... (JNIEnv *, jobject); ...
    (microsoft.public.dotnet.framework.interop)