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");
}
}
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)
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:
Bookmarks