CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2012
    Posts
    2

    JNI Error in passing array

    I'm new to JNI programs. I'm doing one JNI code in which I need to pass array value from C to Java. While displaying in java the previous values are considered in array. As i'm new to JNI i couldn't solve this. This is the code:

    FieldAccess.java

    Code:
    public class FieldAccess { 
      public static void main(String args[]) { 
        next[] Nxt; 
        next n=new next(); 
        n.h=new String[2]; 
        Nxt=n.arrayFields(); 
        for(int i=0;i<Nxt.length;i++) { 
          System.out.println("In Java i= "+i+" g Val= "+Nxt[i].g); 
          for(int j=0;j<2;j++) { 
            System.out.println("In Java i= "+i+" h Val= "+Nxt[i].h[j]); 
          } 
        } 
      } 
      static { 
        System.out.println("Hello"); 
        System.loadLibrary("FieldAccess"); 
      } 
    }
    next.java

    Code:
    public class next{ 
      public String g; 
      public String h[]; 
      public  native next[] arrayFields(); 
      static { 
        System.out.println("Hello"); 
        System.loadLibrary("FieldAccess"); 
      } 
    }
    FieldAccess.c

    Code:
    JNIEXPORT jobjectArray JNICALL Java_next_arrayFields(JNIEnv *env, jobject jobj) { 
      jobjectArray ret; 
      int i,j; 
      printf("Hello"); 
      jclass cls = (*env)->FindClass(env,"next"); 
      ret= (jobjectArray)(*env)->NewObjectArray(env,50,cls,NULL); 
      if(ret==NULL) { 
        ret=NULL; 
      } 
      for(i=0;i<10;i++) { 
        jobject remoteNode = (*env)->AllocObject(env,cls); 
        if (remoteNode == NULL) { 
          ret = NULL; 
        } 
        jfieldID fid,fid1; 
        fid = (*env)->GetFieldID(env,cls, "g", "Ljava/lang/String;"); 
        fid1 = (*env)->GetFieldID(env,cls, "h", "[Ljava/lang/String;"); 
        if (fid == NULL) { 
          ret = NULL; 
        } 
        if (fid1 == NULL) { 
          ret = NULL; 
        } 
        jstring kk= (*env)->NewStringUTF(env,"Hi"); 
        (*env)->SetObjectField(env,remoteNode, fid,(jstring)kk);     
        jstring str[2]; 
        for(j=0;j<2;j++) { 
          str[0]=(*env)->NewStringUTF(env,"jj"); 
          str[1]=(*env)->NewStringUTF(env,"Hello"); 
        } 
        (*env)->SetObjectField(env,remoteNode, fid1, (jstring)str); 
        (*env)->SetObjectArrayElement(env,ret, i, remoteNode); 
      } 
      return ret; 
    }
    The output I'm getting is:

    Code:
    In Java i= 0 g Val= Hi 
    In Java i= 0 h Val= Hi 
    In Java i= 0 h Val= jj 
    In Java i= 1 g Val= Hi 
    In Java i= 1 h Val= Hi 
    In Java i= 1 h Val= jj 
    In Java i= 2 g Val= Hi 
    In Java i= 2 h Val= Hi 
    In Java i= 2 h Val= jj 
    In Java i= 3 g Val= Hi 
    In Java i= 3 h Val= Hi 
    In Java i= 3 h Val= jj 
    In Java i= 4 g Val= Hi 
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out                           
    of range: -253510696 
    at java.lang.String.getChars(String.java:724) 
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:394) 
    at java.lang.StringBuilder.append(StringBuilder.java:120) 
    at FieldAccess.main(FieldAccess.java:94)
    Please any one help

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JNI Error in passing array

    It's a long time since I did any JNI so this advice may not be correct but it looks to me like you aren't setting the h array correctly. I think you need to assign a new object array of type string to h ie:

    Code:
    fid1 = (*env)->NewObjectArray(env, 2, env->FindClass("java/lang/String"), NULL);
    and fill it from str ie
    Code:
    (*env)->SetObjectArrayRegion(env, fid1, 0, 2, str);
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jun 2012
    Posts
    2

    Re: JNI Error in passing array

    There is no functions in JNI like SetObjectArrayRegion. Give me any other option.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JNI Error in passing array

    Sorry, I did say it had been a long time since I did any of this.
    I think the problem is with the line
    Code:
     (*env)->SetObjectField(env,remoteNode, fid1, (jstring)str);
    I'm just not sure what you need to do to sort it. Maybe try iterating over str and use SetObjectArrayElement to set each array element.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured