0

This is my function that, given an object and a field, sets an array into this field with the giveen array

static void createFillAndSetIntArrayField(JNIEnv *env, jobject *javaInstance, jfieldID* field, int *array, int arrayLength)
    {
        jintArray intJavaArray = env->NewIntArray(arrayLength);
        env->SetIntArrayRegion(intJavaArray, 0, arrayLength, array);
        env->SetObjectField(*javaInstance, *field, intJavaArray);
    }

and I'm calling like this

JniUtils::createFillAndSetIntArrayField(env, &pixelFormatInstance, &yuvInternalFormat, pixelFormat_->yuvInternalFormat, 3);

But in Java I get an array of all 0s

I'm sure that pixelFormatInstance exists. What am I possibly doing wrong?

UPDATE:

Following the suggestions:

static void createFillAndSetIntArrayField(JNIEnv *env, jobject *javaInstance, jfieldID *field, int *array, int arrayLength)
    {
        jintArray intJavaArray = env->NewIntArray(arrayLength);
        jint fill[arrayLength];
        for (int i = 0; i < arrayLength; i++)
        {
            fill[i] = array[i];
        }
        env->SetIntArrayRegion(intJavaArray, 0, arrayLength, fill);
        env->SetObjectField(*javaInstance, *field, intJavaArray);
    }

But I'm still getting 0

Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
  • You need to populate each item - your code just populates the first item. See https://stackoverflow.com/questions/1610045/how-to-return-an-array-from-jni-to-java – cup Nov 01 '19 at 05:25
  • @cup im still receiving 0, can you see my update? – Guerlando OCs Nov 01 '19 at 05:51
  • fill[arraylength] is a C99 feature that does not exist in C++. Try changing the file to a C file. Alternatively if you have to use C++ use new and delete on the fill array. – cup Nov 01 '19 at 10:45

0 Answers0