Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7009365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:51:10+00:00 2026-05-27T21:51:10+00:00

I am trying to return a newly created java class object in native to

  • 0

I am trying to return a newly created java class object in native to android. After returning the object I am facing a crash when I am trying to access the internal objects of the returned object.

As of now I am just using the NewObject() function and then setting the parameters and then returning from the native (c language) function.

Do I need to use any other function for informing the JVM and also to make that object Garbage collected?

I have uploaded whole project (Eclipse project) in the eSnips here. Click here for the android project. So that you can download and have a look at your code.

DataObject2.java class object will be returned from the native.

UserDefinedObj.java is

package example.nativeobj.obj;

public class UserDefinedObj {
    public int intPrimitive;

    @Override
    public String toString() {
        StringBuffer strBuf = new StringBuffer();

        strBuf.append("UserDefinedObj.intPrimitive: ");
        strBuf.append(intPrimitive);

        return strBuf.toString();
    }
}

When DataObject2.java is

package example.nativeobj.obj;

public class DataObject2 {
    public int intData;
    public String strData;
    public UserDefinedObj usrDefData;

    @Override
    public String toString() {
        StringBuffer strBuf = new StringBuffer();

        strBuf.append("\n DataObject2.intData: ");
        strBuf.append(intData);
        strBuf.append("\n DataObject2.strData: ");
        strBuf.append(strData);
        strBuf.append("\n DataObject2.usrDefData: ");
        strBuf.append(usrDefData);
//      strBuf.append("\n UserDefinedObj.intPrimitive: ");
//      strBuf.append(usrDefData.intPrimitive);

        return strBuf.toString();
    }
}

I am facing the crash issue.

But when DataObject.java is

package example.nativeobj.obj;

public class DataObject2 {
    public int intData;
    public String strData;
    public UserDefinedObj usrDefData;

    @Override
    public String toString() {
        StringBuffer strBuf = new StringBuffer();

        strBuf.append("\n DataObject2.intData: ");
        strBuf.append(intData);
        strBuf.append("\n DataObject2.strData: ");
        strBuf.append(strData);
        strBuf.append("\n DataObject2.usrDefData: ");
//      strBuf.append(usrDefData);
        strBuf.append("\n UserDefinedObj.intPrimitive: ");
        strBuf.append(usrDefData.intPrimitive);

        return strBuf.toString();
    }
}

I am not facing the crash issue.

Difference is in this part of the code mentioned below, please observe.

//      strBuf.append(usrDefData);
        strBuf.append("\n UserDefinedObj.intPrimitive: ");
        strBuf.append(usrDefData.intPrimitive);

and

        strBuf.append(usrDefData);
//      strBuf.append("\n UserDefinedObj.intPrimitive: ");
//      strBuf.append(usrDefData.intPrimitive);

will anyone please let me know the error in my code.

This is my native code:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class example_nativeobj_api_Native */

#include "custom_debug.h"

//Paths
static const char *gDataObject1ClassPath = "example/nativeobj/obj/DataObject1";
static const char *gDataObject2ClassPath = "example/nativeobj/obj/DataObject2";
static const char *gUserDefinedObjectClassPath = "example/nativeobj/obj/UserDefinedObj";

/*
 * Class:     example_nativeobj_api_Native
 * Method:    func
 * Signature: (Lexample/nativeobj/obj/DataObject1;)Lexample/nativeobj/obj/DataObject2;
 */
JNIEXPORT jobject JNICALL Java_example_nativeobj_api_Native_func
  (JNIEnv *env, jclass cls, jobject dataobj1) {

    jobject dataObject2Obj = NULL;

    /* Get a reference to obj’s class */
    jclass dataObject1Cls = (*env)->GetObjectClass(env, dataobj1);
    jclass dataObject2Cls = (*env)->FindClass(env, gDataObject2ClassPath);
    jclass userDefinedObjCls = (*env)->FindClass(env, gUserDefinedObjectClassPath);

    DBG_LOG_INFO("func: clazz references of the dataObject1Cls: %u, dataObject2Cls: %u, userDefinedObjCls: %u",
                    dataObject1Cls, dataObject2Cls, userDefinedObjCls);
    if(dataObject1Cls == NULL || dataObject2Cls == NULL || userDefinedObjCls == NULL) {
        ERROR_LOG_INFO("func: Unable to get the clazz references of the dataObject1Cls: %u, dataObject2Cls: %u, userDefinedObjCls: %u",
                dataObject1Cls, dataObject2Cls, userDefinedObjCls);
    }

    //Get the field id of the User defined object class
    jfieldID userDefObjintFid;
    userDefObjintFid = (*env)->GetFieldID(env, userDefinedObjCls, "intPrimitive", "I");
    if (userDefObjintFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the intData of the userDefinedObjCls");
        return NULL; /* failed to find the field */
    }



    //Get the data from the dataobject1 class
    ////Get the object class
    jfieldID dataObj1IntFid; /* store the field ID */
    jfieldID dataObj1StrFid;
    jfieldID dataObj1UsrDefObjFid;

    jstring jstrDataObj1 = NULL;
    const char *strDataObj1 = NULL;
    jobject userDefObj = NULL;
    jint iData1ObjInt = 0;
    jint iUsrDefObjInt = 0;

    jthrowable exc;

    /* Look for the instance field s in cls */
    dataObj1IntFid = (*env)->GetFieldID(env, dataObject1Cls, "intData", "I");
    if (dataObj1IntFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the intData of the dataObject1Cls");
        return NULL; /* failed to find the field */
    }

    dataObj1StrFid = (*env)->GetFieldID(env, dataObject1Cls, "strData", "Ljava/lang/String;");
    if (dataObj1StrFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the strData of the dataObject1Cls");
        return NULL; /* failed to find the field */
    }

    dataObj1UsrDefObjFid = (*env)->GetFieldID(env, dataObject1Cls, "usrDefData", "Lexample/nativeobj/obj/UserDefinedObj;");
    if (dataObj1UsrDefObjFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the usrDefData of the dataObject1Cls");
        return NULL; /* failed to find the field */
    }

    ////Get the values of the data members
    iData1ObjInt = (*env)->GetIntField(env, dataobj1, dataObj1IntFid);
    DBG_LOG_INFO("func: iData1ObjInt: %d", iData1ObjInt);

    jstrDataObj1 = (*env)->GetObjectField(env, dataobj1, dataObj1StrFid);
    strDataObj1 = (*env)->GetStringUTFChars(env, jstrDataObj1, NULL);
    if (strDataObj1 == NULL) {
        ERROR_LOG_INFO("func: Unable to get the string of dataObject1Cls");
        return NULL;
    }
    DBG_LOG_INFO("func: c.s = \"%s\"\n", strDataObj1);

    ////Get the user defined object here
    userDefObj = (*env)->GetObjectField(env, dataobj1, dataObj1UsrDefObjFid);
    if(userDefObj == NULL) {
        ERROR_LOG_INFO("func: Unable to get the userDefObj from dataObject1Cls");
        goto cleanup1;
    }

    //////Get the data memebers of the user define object
    iUsrDefObjInt = (*env)->GetIntField(env, userDefObj, userDefObjintFid);
    DBG_LOG_INFO("func: iUsrDefObjInt: %d", iUsrDefObjInt);

    //Creating the new dataobject2 for returning from the function
    ////Get the method id for the constructor of the class
    jmethodID constructorId = (*env)->GetMethodID(env, dataObject2Cls, "<init>", "()V");
    if(constructorId == NULL) {
        ERROR_LOG_INFO("func: Unable to get the constructor for the dataObject2Cls");
        goto cleanup1;
    }

    ////Creating the new object for the class
    dataObject2Obj = (*env)->NewObject(env, dataObject2Cls, constructorId);
    if(dataObject2Obj == NULL) {
        ERROR_LOG_INFO("func: Unable to create an object for the class dataObject2Cls");
        exc = (*env)->ExceptionOccurred(env);
        if (exc) {
            /* We don't do much with the exception, except that
            we print a debug message for it, clear it, and
            throw a new exception. */
            (*env)->ExceptionDescribe(env);

        }
        goto cleanup1;
    }


    //Create the new dataobject2
    ////Get the field id's of the objectdata2 members
    jfieldID dataObj2IntFid; /* store the field ID */
    jfieldID dataObj2StrFid;
    jfieldID dataObj2UsrDefObjFid;

    dataObj2IntFid = (*env)->GetFieldID(env, dataObject2Cls, "intData", "I");
    if (dataObj2IntFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the intData of the dataObject2Cls");
        goto cleanup1; /* failed to find the field */
    }

    dataObj2StrFid = (*env)->GetFieldID(env, dataObject2Cls, "strData", "Ljava/lang/String;");
    if (dataObj2StrFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the strData of the dataObject2Cls");
        goto cleanup1; /* failed to find the field */
    }

    dataObj2UsrDefObjFid = (*env)->GetFieldID(env, dataObject2Cls, "usrDefData", "Lexample/nativeobj/obj/UserDefinedObj;");
    if (dataObj2UsrDefObjFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the usrDefData of the dataObject2Cls");
        goto cleanup1; /* failed to find the field */
    }

    ////Set the data member values
    //////Set the int field
    (*env)->SetIntField(env, dataObject2Obj, dataObj2IntFid, (iData1ObjInt+1));
    //////Set the string field
    jstring jstr_temp = (*env)->NewStringUTF(env, strDataObj1);
    if (jstr_temp == NULL) {
        ERROR_LOG_INFO("func: Unable to create the new string utf for the dataobj2class object");
        goto cleanup1; /* out of memory */
    }
    (*env)->SetObjectField(env, dataObject2Obj, dataObj2StrFid, jstr_temp);
    //////Set teh user define object
    ////////Get the method id for the constructor of the user defined class
    jmethodID usrDefConstructorId = (*env)->GetMethodID(env, userDefinedObjCls, "<init>", "()V");
    if(usrDefConstructorId == NULL) {
        ERROR_LOG_INFO("func: Unable to get the constructor for the dataObject2Cls");
        goto cleanup1;
    }

    ////////Creating the new object for the class
    jobject userDefObject = (*env)->NewObject(env, dataObject2Cls, usrDefConstructorId);
    if(!userDefObject) {
        ERROR_LOG_INFO("func: Unable to create an object for the class userDefObject");
        goto cleanup1;
    }
    DBG_LOG_INFO("func: userDefObject: %u", userDefObject);
    DBG_LOG_INFO("func: userDefObjintFid: %u", userDefObjintFid);
    DBG_LOG_INFO("func: dataObj2UsrDefObjFid: %u", dataObj2UsrDefObjFid);

    iUsrDefObjInt += 1;
    DBG_LOG_INFO("func: iUsrDefObjInt: %d", iUsrDefObjInt);
    (*env)->SetIntField(env, userDefObject, userDefObjintFid, iUsrDefObjInt);
    ////////Set this user defined object in the data object 2 class
    (*env)->SetObjectField(env, dataObject2Obj, dataObj2UsrDefObjFid, userDefObject);

    //////////////////////////////////////////////////////////////////////////
    //For verifying if the user defined object is set
//  jobject userDefObj_Temp = (*env)->GetObjectField(env, dataObject2Obj, dataObj2UsrDefObjFid);
//  if(userDefObj == NULL) {
//      ERROR_LOG_INFO("func: Unable to get the userDefObj from dataObject1Cls");
//      goto cleanup1;
//  }
//
//  //////Get the data memebers of the user define object
//  int iUsrDefObjInt_temp = (*env)->GetIntField(env, userDefObj_Temp, userDefObjintFid);
//  DBG_LOG_INFO("func: iUsrDefObjInt_temp: %d", iUsrDefObjInt_temp);

    //////////////////////////////////////////////////////////////////////////



    cleanup1:
    //Release all the handlers
    (*env)->ReleaseStringUTFChars(env, jstrDataObj1, strDataObj1);


    //return the dataobject 2
    return dataObject2Obj;
}


JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {

    JNIEnv *env = NULL;

    DBG_LOG_INFO("JNI_OnLoad: Called in libobjnative.so.....");

    if((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_4) != JNI_OK) {
        ERROR_LOG_INFO("JNI_OnLoad: Failed to get the environment ising GetEnv");
        return -1;
    }


    return JNI_VERSION_1_4;
}


JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserved) {

    DBG_LOG_INFO("JNI_OnUnload: Called in libobjnative.so.....");

    return;
}

Thanks & Regards,
SSuman185

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:51:11+00:00Added an answer on May 27, 2026 at 9:51 pm

    Found the problem.

    It was the problem while creating the object of UserDefinedObj

    In the native code I was doing like this

    jobject userDefObject = (*env)->NewObject(env, dataObject2Cls, usrDefConstructorId);
    

    Was supplying the reference of the “dataObject2Cls” for the “UserDefinedObj”

    Got fixed on supplying the “userDefinedObjCls”

    jobject userDefObject = (*env)->NewObject(env, userDefinedObjCls, usrDefConstructorId);
    

    Regards,
    SSuman185

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to redirect a user to a newly created object's object.get_absolute_url() after saving
I am trying to pull the id from the newly created project object so
I'm trying to return an iterator for a vector in a templated class (I'm
Hey, I am trying to return a user defined class from a web method.
I'm trying to overload the instream operator >> for a fraction class. I've created
I am trying to retrieve the generated ID of a newly created Entity within
im trying to return a string value from a method inside my script tag
Im trying to return the value that a $ajax call returns, from a function
Im trying to return a SimpleQuery list that queries a single table and uses
Am trying to return the sum of each day of a week in mysql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.