0

i have trying to pass array from c++ to java. no error in compilation. but failed to run (crash). when i debug the code, error occurs in return code

i try to implement this code how to return c++ char 2d array using JNI to JAVA

here is my code

    JNIEXPORT jobjectArray JNICALL my_function();
    // ...
    // some code
    // ...

    // i want convert float point[3][2] to java

    jclass intArray1DClass = env->FindClass("[I");
    jclass intArray2DClass = env->FindClass("[[I");

     //  float point[3][2]
     jint sizeX = 3;
     jint sizeY = 2;


    jobjectArray array2D = env->NewObjectArray(
                sizeX, intArray2DClass, NULL);

    for (jint i = 0; i < sizeX; i++)
    {
        jobjectArray array1D = env->NewObjectArray(
                    sizeY, intArray1DClass, NULL);

        for (jint y = 0; y < sizeY; y++)
        {
            jfloatArray value = env->NewFloatArray(point[i][y]); // float point[3][2]
            env->SetObjectArrayElement(array1D, y, value);
        }

        env->SetObjectArrayElement(array1D, i, array1D);
    }

    return array2D;

and this is how i call function from java. is it right?

     float[][] point = my_function()

thank you

Update

Finally i use 1D array because its simple to write the code. and according to 1D or 2D array, what's faster? , we should use 1D array

     jint sizeX = 6;

     jfloatArray array1D = env->NewFloatArray(sizeX);

     env->SetFloatArrayRegion(array1D, 0, sizeX, point);


     return array1D;
Angga Arya S
  • 137
  • 1
  • 11

1 Answers1

3

First, it looks like you declaring Java array of ints, i.e. int [][], not float[][].

Second, why you set jfloatArray as element of your array? It should be simple float.

Third, in last line you probably meant env->SetObjectArrayElement(array2D, i, array1D);

Fourth, when you declaring array you need to specify class which it contains, so for 2D array it contains 1D array and for 1D array it contains just floats.

PS: Linked question/answer is actually returning 3D array. So you need to reduce number of dimensions in you code.

sklott
  • 2,634
  • 6
  • 17