How to initialize a jintArray ?
2 posts in topic
Flat View  Flat View

Posted On:   Friday, February 8, 2002 12:27 AM

I have a CPP program, In that I want to initialize the jintArray. I constructed it using NewIntArray(5). But I want to assign some values to this array.

My code:

jintArray array = env->NewIntArray(5);

After this I retrieved the value by,
jint value = env->GetObjectArrayElement((jobjectArray)array,(jsize)0);

This returns zero itself, eventhough I set to 1500.

Is this correct ?
If not, Please give a solution early.


Re: How to initialize a jintArray ?

Posted By:   Alexander_Krapf  
Posted On:   Thursday, March 14, 2002 03:19 PM


You should not be using the SetObjectArrayElement and GetObjectArrayElement methods at all. These methods are for arrays of objects, not for arrays of integers. There are other variants for primitive types.

You might also be interested in our JunC++ion tool, which makes all these details unnecessary by giving you nice, easy to use C++ code instead. JunC++ion takes Java classes as input and generates C++ proxy classes. In the case of arrays of primitives you would write:

jcpp_int::array1D arr( 5 );

arr[ 0 ] = 1500;
cout << arr[ 0 ] << endl;

You could even do this:

jcpp_int::array1D arr( 5 );

for( int i=0; i; i++ )
arr[ 0 ] = 1500;

Find out more about JunC++ion at www.codemesh.com.

Good luck,


Re: How to initialize a jintArray ?

Posted By:   Christopher_Schultz  
Posted On:   Friday, February 8, 2002 05:23 AM

I don't know the answer to this specific question, but I do know one thing that might bite you in the end (and it might solve your problem, here... who knows).

In JNI, your arrays have to be 1 index bigger than you actually want it to be. This is because Java uses the last array element to identify the type of the array (String[] versus int[]).

Make sure that you set array[4] equal to the correct type (which will be int in this case).

Good luck,

About | Sitemap | Contact