• Post Reply Bookmark Topic Watch Topic
  • New Topic

JNI  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Currently i am working on JNI for native access between java and c++.

Here goes my doubt:

If i have to access an integer field (present inside my java class) in my c++ code, then the code is like this:


But my problem is, if in the above case i have an integer array (instead of just an int),
and i want to perform similar get and set operations on the array, then what would be the code?

I am trying something like this:

But, it doesn't work and instead gives error if i try to operate on the array 'ByteArr '.

It would be really great, if someone can fix this problem.

With regards,
sattu
 
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

you should not use AllocObject, as that does not call any of the class constructors. Use GetMethodID to get the constructor to use, using "<init>" as the method name, then use NewObject, NewObjectA or NewObjectV to call that constructor.

Because you used AllocObject the byte array is not instantiated and therefore still null.
 
satya prakash panigrahi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Welcome to the Ranch!

You should not use AllocObject, as that does not call any of the class constructors. Use GetMethodID to get the constructor to use, using "<init>" as the method name, then use NewObject, NewObjectA or NewObjectV to call that constructor.

Because you used AllocObject the byte array is not instantiated and therefore still null.


Thanks Rob,
But i couldn't get you properly. I mean, by using AllocObject, i am able to operate on the primitive variables(like int or boolean) present inside a java class. But then, as you said, it will not work for arrays as they are not instantiated. Why?
Like that also, I haven't put any constructors inside my class in which both this variables(var and raw) are present. So are you speaking of the default constructor to be initialized by calling GetMethodID? If so, what would be it's signature? I guess, it's "()V".

Sorry, if i am being over-descriptive, but i am both new to Java and JNI, so this confusion.

Waiting for your reply,
 
satya prakash panigrahi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

I finally solved it by the way you told, first calling the constructor by using env->GetMethodID and env->NewObject method and then using env->GetObjectField and env->SetObjectField method to perform the operations on array.

Thanks, thanks, thanks, thanks.

Believe me, i had been trying this for the whole day, but couldn't guess that the entire problem was lying in env->AllocObject method.
Wow, now feeling relieved that i can move forward.

But still couldn't get one thing, with env-AllocObject i was able to operate on the primitive variables present inside the class, but not on the arrays. Why? What has the default constructor got to do here?

Thanks once again for your timely help,
Waiting for your reply,
 
satya prakash panigrahi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
You should not use AllocObject, as that does not call any of the class constructors. Use GetMethodID to get the constructor to use, using "<init>" as the method name, then use NewObject, NewObjectA or NewObjectV to call that constructor.

Because you used AllocObject the byte array is not instantiated and therefore still null.


Sorry, but now i am completely fixed in string. I mean, like the bytearray raw, i am also having a String called str in my java class. But however hard i try, i am not able to change it's value.
I am trying this way:


Now, I am trying to change the value of str in following ways:


OR, like this:


By using SetObjectField, I am able to change the values inside my byte array, but it's not happening for the String variable.
I don't know what is wrong, but str value remains NULL even after putting "hellohi" inside it.

Please help me,
Thanks in advance,
 
Rob Spoor
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satya prakash panigrahi wrote:But still couldn't get one thing, with env-AllocObject i was able to operate on the primitive variables present inside the class, but not on the arrays. Why? What has the default constructor got to do here?

I take it you initialized your array directly when you declared it? In other words, something like this:
You could access the primitive fields because the memory for their values is available, it's just been unassigned. The memory for the byte[] is also available, just not as the byte[] itself but as a reference to the byte[]. And that reference is still null since the assignment never happened.

satya prakash panigrahi wrote: I am trying this way:


Now, I am trying to change the value of str in following ways:


OR, like this:


By using SetObjectField, I am able to change the values inside my byte array, but it's not happening for the String variable.
I don't know what is wrong, but str value remains NULL even after putting "hellohi" inside it.

Retrieving it is fine indeed.
The first code for setting only changes a local variable of the JNI code. It's not the same as the actual reference in Java.
The second code almost works. You're calling the right JNI function, you just need to fix the parameters. The parameters should be the object to set the field for, the field ID and the value. In other words:
 
satya prakash panigrahi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bingo, You are right. It was some mismatch in the parameters inside SetObjectField method.

Thanks a lot for correcting my mistake.
So, now shall i mark it solved? I mean i may get more doubt regarding this topic only.
 
Rob Spoor
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you can still post in the thread. That remains possible until a staff member closes it, which we don't do unless something's wrong with the thread.
 
satya prakash panigrahi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok boss.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!