JNI: Accessing value in an array  
Author Message
Neena





PostPosted: 2005-9-12 15:01:00 Top

java-programmer, JNI: Accessing value in an array I have a code::


jobject headerVal;
//jsize size = 0;
jclass objClass;
jmethodID mid = NULL;
void * headval;


do
{

/* after this call, headval will contain a pointer to an array.and
HeadID will contain an int value.*/

GetValue( headval,headdID);

if(headval)
{

objClass = (*env)->FindClassenv, "Ljava/lang/Object;");

if(objClass == NULL)
{
break;
}
mid = (*env)->GetMethodID(env, objClass, "<init>", "(J)V");
if(mid == NULL)
{
break;
}
if(headID>0x30 || headID < 0x3F)
{

headerVal = (*env)->NewObject(env, objClass, mid,(jstring)headval);

}

else if(headerID>0x70 || headerID < 0x7F)
{

headerVal = (*env)->NewObject(env, objClass, mid,(jbyteArray)headval);

}
else if(headerID>0xB0 || headerID < 0xBF)
{

headerVal = (*env)->NewObject(env, objClass, mid,
(jbyte)headval);
}
else if(headerID>0xF0 || headerID < 0xFF)
{

headerVal = (*env)->NewObject(env, objClass, mid,
(jlong )headval);
}
}

}while(FALSE);

return headerVal;
}


I know it is not the correct way to assign headerVal. how can i assign
the value contained in array pointed by headval???

I am a beginner in c and dont know much abt pointers...
please do help me...

 
Gordon Beaton





PostPosted: 2005-9-12 19:28:00 Top

java-programmer >> JNI: Accessing value in an array On 12 Sep 2005 00:01:01 -0700, Neena wrote:
> jobject headerVal;
> void * headval;
>
> objClass = (*env)->FindClassenv, "Ljava/lang/Object;");
>
[...]
> mid = (*env)->GetMethodID(env, objClass, "<init>", "(J)V");
[...]

> I know it is not the correct way to assign headerVal. how can i
> assign the value contained in array pointed by headval???

Presumably headerVal is not *really* of type java.lang.Object. Yes,
jobject is a common pointer type for all objects in native code, but
you still need to specify the correct class when you create the
object. Note in particular that Object doesn't have a constructor that
takes a long argument, so GetMethodID() will fail here.

Similarly, headval is apparently some kind of array, but by declaring
it with a generic pointer type void*, you are hiding information about
it from the compiler and from us.

In order to help us answer your question, you need to show us what the
headerVal class looks like (specifically, what its constructors look
like), and you need to show us what headval *really* contains in the
four different cases. We aren't mindreaders!

Also, please don't misuse do { } while(0) just so you can leave the
function early with break. Use return in those places you want to
return from, that's what it's for. If you have to complicate the code
in order to satisfy a coding guideline (such as "use exactly one
return statement") then your efforts (and the guideline) are
misguided.

/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