4 Replies Latest reply: Aug 12, 2011 9:51 AM by BIJ001 RSS

    ArrayList .get(int index) problem

    427038
      Hi,

      I have the following situation:
      I have a Java GUI ( for testing) with a main.
      On the button pressed action method I am creating an ArrayList<Integer> type list with some integers as content.
      The java calls a native C code which need to do some work with each element of array. Somewhere is wrong something, but I don't know where, because it seems all OK for me.
      With java debugger I have look at ArrayList values and on Java side is ok.

      Here is the C code part: -exceptios, error checking, third party missing
      // Signature: (Ljava/util/ArrayList;)V
      JNIEXPORT void JNICALL Java_mypackage_MyClass_myNativeMethod
      (JNIEnv * env, jobject jCalllingObject, jobject alParam)
      {
              jclass clsArrayList = env->FindClass("java/util/ArrayList");
           jmethodID midArrayListSize = 0;
           jmethodID midArrayListGet = 0;
           if(clsArrayList != 0){
                midArrayListSize = env->GetMethodID( clsArrayList, "size", "()I");
                midArrayListGet = env->GetMethodID(clsArrayList, "get", "(I)Ljava/lang/Object;");
           }
           jclass clsInteger = env->FindClass("java/lang/Integer");
           jmethodID midIntegerIntValue = 0;
           if(clsInteger != 0){
                midIntegerIntValue = env->GetMethodID(clsInteger, "intValue", "()I");
           }
      
           if((clsArrayList != 0) && (midArrayListSize != 0)){          
                int arrayListSize = env->CallIntMethod(alParam,  midArrayListSize );//ok
      
                if((arrayListSize > 0)&& (midArrayListGet != 0) && (clsInteger != 0) && (midIntegerIntValue != 0)){          
      
                     for(int i=0; i<arrayListSize; i++){
                          jobject arrayElement = env->CallObjectMethod(alParam,  midArrayListGet);//get: will contain 0 :(
                          int intValue = env->CallIntMethod(arrayElement,  midIntegerIntValue);     //intValue                              
                          //...
                     }
                     //...          
                }
           }
      }
      The first error sign I see at
      jobject arrayElement = ..
      because has the 0 value, like a null pointer.... I have given 2 elements of array eg 400 and 500, so there the 0 isn't good I think.

      What is wrong in this code, how can be correct?