dcsimg
How do I pass data to an array defined in JAVA but initialized and filled in native funtion?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Dieter_Hackl
Posted On:   Friday, July 13, 2001 12:27 AM

Hi folks! I'm passing an int array and string array (both initialized to null) to a native function. On the native side I alloc mem for the 2 arrays and fill them. But on the JAVA side I get a NullPointerException. If I initialize the arrays on the JAVA side ( int[] intArr = new int[100] ) everything works fine! Here's my JAVA code: int[] intArr = null; String[] strArr = null; int numElements = nativeGetElements(intArr,strArr); for(int i=0;i { System.out.println(intArr[i]); System.out.println(strArr[i]); } System.out.println("All done"); ********************   More>>

Hi folks!


I'm passing an int array and string array (both initialized to null) to a native function.

On the native side I alloc mem for the 2 arrays and fill them.

But on the JAVA side I get a NullPointerException.

If I initialize the arrays on the JAVA side ( int[] intArr = new int[100] ) everything works fine!


Here's my JAVA code:


			
int[] intArr = null;
String[] strArr = null;

int numElements = nativeGetElements(intArr,strArr);

for(int i=0;i
{
System.out.println(intArr[i]);
System.out.println(strArr[i]);
}

System.out.println("All done");


*******************************

And here my native code:



			
JNIEXPORT jint JNICALL Some_Class_nativeGetElements
(JNIEnv *env, jobject obj, intArray intArr, jobjectArray strArr)
{
int nElements;
jint *jnIntArr;
jstring *jszStrArr;
char *szHelp = new char[SOME_SIZE];

nElements = CountElementsSomehow();
if(nElements) {
//initialize JAVA arrays (if this is done on JAVA side, it works fine!)
intArr = env->NewIntArray(nElements);
strArr = env->NewObjectArray(nElements,env->FindClass("java/lang/String"),NULL);

//initialize corresponding native types
jnIntArr = new jint[nElements];
jszStrArr = new jstring[nElements];

//fill array
for(int i=0; i < nElements; i++) {
jnIntArr[i]=GetIntSomehow();

GetStringSomehow(szHelp,SOME_SIZE);
jszStrArr[i]=env->NewStringUTF(szHelp);
env->SetObjectArrayElement(strArr,i,jszStrArr[i]);
}
//pass data to JAVA
env->SetIntArrayRegion(intArr,0,nElements,jnIntArr);
env->ReleaseIntArrayElements(intArr,jnIntArr,0);
}
delete szHelp;
//I don't know if I should delete jnIntArr and jszStrArr

return nElements;
}



Please help!


Thanks!

Dieter Hackl

   <<Less

Re: How do I pass data to an array defined in JAVA but initialized and filled in native funtion?

Posted By:   Alexander_Krapf  
Posted On:   Monday, July 16, 2001 09:29 AM

Hi Dieter,


One thing I am noticing is that you are not deleting the object references after you're done with them. You should release all local references that are not directly used as results, i.e. the string class (BTW you should only find that once and cache it for the duration of the method) and the array elements.


You could also check out JunC++ion at http://www.codemesh.com, which would allow you to implement your method as follows:


jint _cmj_Some_Class_nativeGetElements
(jcpp_localenv*env, Some_Class & _this, jcpp_int::array1D & _intArr, java::lang::String::array1D & _strArr)
{
int nElements = CountElementsSomehow();
char szHelp[ BUFFER_SIZE ];

if(nElements)
{
_intArr = jcpp_int::array1D(nElements);
_strArr = String::array1D(nElements);

//fill array
for(int i=0; i< nElements; i++)
{
GetStringSomehow(szHelp,SOME_SIZE);

_intArr[i] = GetIntSomehow();
_strArr[i] = szHelp;
}
}
return nElements;
}




Looks much simpler doesn't it? Contact us for more information.


Regards,


Alex

About | Sitemap | Contact