JNI: Accessing value in an array

N

Neena

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...
 
G

Gordon Beaton

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top