【问题标题】:Index out of bound exception between Java and JNI (dll)Java 和 JNI (dll) 之间的索引越界异常
【发布时间】:2014-07-03 14:07:14
【问题描述】:

我为我的 java 应用程序编写了一个 dll。 dll 中的一种方法获取一些浮点值,将它们存储在数组中,最后将数组发送到 java 应用程序。我的问题是,当我尝试将本机方法的结果存储在位于 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方法执行过程中没有任何问题,但我猜可能是我忘记了一个重要的命令……

【问题讨论】:

    标签: java c++ c arrays dll


    【解决方案1】:

    env-&gt;SetFloatArrayRegion(JNIArray, l, 6, temp); 每次都在尝试写入 6 浮点数。让它1。另外,我假设第二个参数是 i 而不是 l

    【讨论】:

      猜你喜欢
      • 2018-11-19
      • 2013-03-30
      • 1970-01-01
      • 2013-03-05
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多