我已经为我的 java 应用程序编写了一个 dll。 dll 中的一种方法获取一些浮点值,将它们存储在数组中,最后,它将数组发送到 java 应用程序。我的问题是,当我尝试将 native 方法的结果存储在位于 Java 层的数组中时,会抛出索引超出范围异常。以下是代码的一些部分:
//note: MyClass is a static class...
public static void javaMethod() {
float[] Aux = new float[6];
Aux = MyClass.NativeMethod(); //error (index out of bounds exception)
System.arraycopy(MyClass.NativeMethod(), 0, auxArray, 0, 6); //fails as well
}
JNIEXPORT jfloatArray JNICALL Java_Package_MyClass_NativeMethod
(JNIEnv * env, jclass cls)
{
jfloatArray JNIArray = env->NewFloatArray(6);
if(JNIArray == NULL)
return NULL;
for(int i = 0 ; i < 6 ; i++)
{
float * temp;
temp = &gobalArray[i]; //let's say that this is a float array with 6 elements...
env->SetFloatArrayRegion(JNIArray, i, 6, temp);
}
return JNIArray;
}
dll方法执行过程中没有问题,但我猜想我可能忘记了一个重要的命令...
最佳答案
env->SetFloatArrayRegion(JNIArray, l, 6, temp);
每次都尝试写入 6
float 。设为 1
。此外,我假设第二个参数是 i
而不是 l
。
关于java - Java 和 JNI (dll) 之间的索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24555951/