This might sound weird but I am struggling with this bug for past 2 days.
-
I have a
booleanarray in java that is initialised using aRandomboolean generator. -
After that the
booleanarray is acted upon by a function in C (called using JNI) and the modifiedbooleanarray is returned to java. When I hand over thebooleanarray to C, it is converted to unsigned char and converted back tojbooleanArraybefore being handed back to java. -
Now I run the following code (there is a for loop over i):
if(chosen_packet[i] == false) { pkt.first[i] = 0; System.out.print(chosen_packet[i]); } if(chosen_packet[i] == true) { pkt.first[i] = 1; System.out.print(chosen_packet[i]); }
The problem is that sometimes when chosen_packet[i] is true it still does not enter the second if condition. This happens sometimes and sometimes the code works just fine. When I print chosen_packet[i] in such a case it is printed as true yet it does not enter the second if condition. What could be the possible reason for this seeming corruption of the boolean array ?
EDIT: This is how I convert the boolean array to unsigned char in C:
jboolean *element = (*env)->GetBooleanArrayElements(env,chosen_packet,0);
for(j = 0; j < sz; j++)
src_pkt[j] = (unsigned char)element[j];
This src_pkt is acted upon and then I convert it back to jboolean .
EDIT2: This is how I convert the unsigned char array back to jboolean:
jbooleanArray arr = (*env)->NewBooleanArray(env,sz);
(*env)->SetBooleanArrayRegion(env,arr,0,sz,src_pkts);
(*env)->DeleteLocalRef(env,arr);
It’s normal that if the
booleanistrueit does not enter in the firstifsince you are checking for it to befalsein its condition.Your code can be simplified to (you should use
if/elseinstead of two if checking for the different conditions).or even
Edit
If your program does not enter the second if, it means that the the var
chosen_packet[i]is nottrue, you could use a debugger to verify what is the real value.As stated by fredcrs, are you sure that
chosen_packet[i]is of typeboolean?