By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,137 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,137 IT Pros & Developers. It's quick & easy.

JNI: 2 Dimensional Integer Array: ArrayIndexOutOfBoundsException

P: n/a
Programs from: Sheng Liang, The Java Native Interface, Programmer's
Guide and Specification, The Java Series, (c) 1999, pp. 38-39.

I thought I copied the program pretty exactly, but I don't know
why I am getting an ArrayIndexOutOfBoundsException.

I tried to switch the jint size to int size in the function declaration
prototype, but that didn't make any difference. I tried the "i" for loop
with a range of i = 1 ; i <= size ; but that didn't help.

Any help would be appreciated. Thanks.

David Stevenson

bash -v buildObjectArrayTest.sh
javac ObjectArrayTest.java
javah -jni ObjectArrayTest
gcc -c -ansi -shared -I/usr/java/j2sdk1.4.2_06/include
-I/usr/java/j2sdk1.4.2_06/include/linux -I-. ObjectArrayTest.c
gcc -shared ObjectArrayTest.o -o libObjectArrayTest.so
java -Djava.library.path=. ObjectArrayTest
Java_ObjectArrayTest_initInt2DArray entered
Java_ObjectArrayTest_initInt2DArray after FindClass
Java_ObjectArrayTest_initInt2DArray before NewObjectArray
Java_ObjectArrayTest_initInt2DArray after NewObjectArray
size: 3
i: 0
before NewIntArray
after NewIntArray
before SetIntArrayRegion
before SetObjectArrayElement
before DeleteLocalRef
after DeleteLocalRef
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at ObjectArrayTest.initInt2DArray(Native Method)
at ObjectArrayTest.main(ObjectArrayTest.java:7)
$ more ObjectArrayTest.java
public class ObjectArrayTest
{
private static native int [] [] initInt2DArray ( int size ) ;

public static void main ( String [] args )
{
int [] [] i2arr = initInt2DArray ( 3 ) ;
for ( int i = 0 ; i < 3 ; i++ )
{
for ( int j = 0 ; j < 3 ; j++ )
{
System.out.print ( " " + i2arr[i][j] ) ;
}
System.out.println () ;
}
}

static
{
System.loadLibrary ( "ObjectArrayTest" ) ;
}
}
#include <jni.h>
#include <stdio.h>
#include "ObjectArrayTest.h"

/*
* Class: ObjectArrayTest
* Method: initInt2DArray
* Signature: (I)[[I
*/
JNIEXPORT jobjectArray JNICALL
Java_ObjectArrayTest_initInt2DArray ( JNIEnv *env, jclass cls, int size )
{
jobjectArray result ;
int i ;
printf ( "Java_ObjectArrayTest_initInt2DArray entered\n" ) ;
jclass intArrCls = (*env)->FindClass ( env, "[I" ) ;
printf ( "Java_ObjectArrayTest_initInt2DArray after
FindClass\n" ) ;
if ( intArrCls == NULL )
return NULL ; /* exception thrown */

printf ( "Java_ObjectArrayTest_initInt2DArray before
NewObjectArray\n" ) ;
result = (*env)->NewObjectArray ( env, size, intArrCls, NULL ) ;
if ( result == NULL )
return NULL ; /* out of memory error thrown */
printf ( "Java_ObjectArrayTest_initInt2DArray after
NewObjectArray\n" ) ;

printf ( "size: %d\n", size ) ;

for ( i = 0 ; i < size ; i++ )
{
printf ( "i: %d\n", i ) ;

jint tmp [ 256 ] ; /* make sure it is large enough */
int j ;
printf ( "before NewIntArray\n" ) ;
jintArray iarr = (*env)->NewIntArray ( env, size ) ;
if ( iarr == NULL )
return NULL ;
printf ( "after NewIntArray\n" ) ;
for ( j = 0 ; i < size ; i++ )
tmp [ j ] = i + j ;
printf ( "before SetIntArrayRegion\n" ) ;
(*env)->SetIntArrayRegion ( env, iarr, 0, size, tmp ) ;
printf ( "before SetObjectArrayElement\n" ) ;
(*env)->SetObjectArrayElement ( env, result, i, iarr ) ;
printf ( "before DeleteLocalRef\n" ) ;
(*env)->DeleteLocalRef ( env, iarr ) ;
printf ( "after DeleteLocalRef\n" ) ;
}
return result ;
}

more ObjectArrayTest.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ObjectArrayTest */

#ifndef _Included_ObjectArrayTest
#define _Included_ObjectArrayTest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ObjectArrayTest
* Method: initInt2DArray
* Signature: (I)[[I
*/
JNIEXPORT jobjectArray JNICALL Java_ObjectArrayTest_initInt2DArray
(JNIEnv *, jclass, jint);

#ifdef __cplusplus
}
#endif
#endif
Jan 12 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Thu, 12 Jan 2006 02:58:47 GMT, David Stevenson wrote:
I thought I copied the program pretty exactly, but I don't know why
I am getting an ArrayIndexOutOfBoundsException.
[...]

I believe the following inner loop is causing the problem:
for ( j = 0 ; i < size ; i++ )
tmp [ j ] = i + j ;


Check your index variables carefully...

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Jan 12 '06 #2

P: n/a
Gordon Beaton wrote:
On Thu, 12 Jan 2006 02:58:47 GMT, David Stevenson wrote:
I thought I copied the program pretty exactly, but I don't know why
I am getting an ArrayIndexOutOfBoundsException.

[...]

I believe the following inner loop is causing the problem:

for ( j = 0 ; i < size ; i++ )
tmp [ j ] = i + j ;

Check your index variables carefully...

/gordon


Thanks for catching that.

The corrected code:

for ( j = 0 ; j < size ; j++ )
tmp [ j ] = i + j ;

David Stevenson
Jan 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.