473,503 Members | 12,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JNI: 2 Dimensional Integer Array: ArrayIndexOutOfBoundsException

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
2 9804
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6392
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
6
1617
by: portroe | last post by:
my 2-dimensional array, is given me the following error message, 'For' loop control variable cannot be of type '1-dimensional array of String'. has anybody got a tip on what I have...
4
5870
by: rmorvay | last post by:
I have a requirement to search a multi-dimensional array for an item, then delete the item and "reset" the array so that their are no gaps in the resulting array. I have been trying to figure out...
16
4375
by: rguti | last post by:
Hi, How do I create a two dimensional array? I have created a one dimensional doing this: Dim laFields As ArrayList = New ArrayList How about to do a 2 dimensional?
4
2590
by: Tad Marshall | last post by:
Hi, I'm reading about arrays in VB.NET and I seem to have a few options for my data structure. I need a multi-dimensional array of structures, and my first thought was Public Structure myStr...
4
6360
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
5
3865
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six...
152
9733
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
2
6636
myusernotyours
by: myusernotyours | last post by:
Hi All, Am working on a Java application in which I have to use the JNI to Interface with some native code for both windows and unix. Am using netbeans IDE with the C/C++ pack installed. Am also...
0
7212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.