Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

JNI: 2 Dimensional Integer Array: ArrayIndexOutOfBoundsException

Thread view: 
David Stevenson - 12 Jan 2006 02:58 GMT
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
Gordon Beaton - 12 Jan 2006 08:55 GMT
> 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

Signature

[  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

David Stevenson - 12 Jan 2006 23:20 GMT
>>I thought I copied the program pretty exactly, but I don't know why
>>I am getting an ArrayIndexOutOfBoundsException.
[quoted text clipped - 9 lines]
>
> /gordon

Thanks for catching that.

The corrected code:

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

David Stevenson


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.