NewObject() fails

Hi all,

I have a very strange problem with JNI

when I call NewObject()

function the caller function returns immediately with no indication about what happens (exception or whatever).

the specification says that if the object can not be instantiated NULL should be returned! but this doesn't happen with me, I am completely unable to know what happens and make the caller function returns immadiately (I mean, the following lines aren't executed)

I also want to mention that the caller function is a callback which runs on another thread but I attached the thread to the VM using AttachCurrentThread()

function,

here a piece of my code

JNIEnv *jniEnv;

javaVM->AttachCurrentThread((void**)&jniEnv,NULL);

cout<<"thread attached"<<endl;

jclass remoteDeviceClass=jniEnv->FindClass("javax/bluetooth/RemoteDevice");

if(remoteDeviceClass==NULL)

cout<<"can not find class"<<endl;

else

cout><<"class found"<<endl;

jmethodID remoteDeviceConstructor=jniEnv->GetMethodID(remoteDeviceClass,"<init>","([BLjava/lang/String;)V");

if(remoteDeviceConstructor==NULL)

cout<<"can not find constructor"<<endl;

else

cout><<"constructor found"<<endl;

jobject remoteDevice=jniEnv->NewObject(remoteDeviceClass,remoteDeviceConstructor,convertAddressToJByteArray(bda),jniEnv->NewStringUTF((char*)bdName));// this line make the function return immediately with no exception or any indicator of the problem, the following lines are never executed.

if(remoteDevice==NULL)

cout<<"can not instantiate RemoteDevice"<<endl;

else

cout><<"RemoteDevice instantiated"<<endl;

cout><<"ok"<<endl;

jclass discoveryListenerClass=jniEnv->GetObjectClass(discoveryListener);

jmethodID deviceDiscovered=jniEnv->GetMethodID(discoveryListenerClass,"deviceDiscovered","(Ljavax/bluetooth/RemoteDevice;Ljavax/bluetooth/DeviceClass;)V");

jniEnv->CallVoidMethod(discoveryListener,deviceDiscovered,remoteDevice,NULL);

javaVM->DetachCurrentThread();

and I got output like that

thread attached

class found

constructor found

thanks in advance

[2902 byte] By [minashokrya] at [2007-11-27 0:51:00]
# 1

I assume your constructor takes a bytearray and a string as arguments.

try this:

jmethodID remoteDeviceConstructor=jniEnv->GetMethodID(remoteDeviceClass, "<init>", "()V");

I think this should fix the issue.

If that does not work, try creating an empty object and populate the arguments separately.

jobject remoteDevice=jniEnv->NewObject(remoteDeviceClass,remoteDeviceConstructor);

and then populate each individual field using setByteArrayRegion and setObjectFied..I think you can figure out how to do this ..Good luck.

kteegalaa at 2007-7-11 23:21:36 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

thank you for your interest,

but the problem is still same after trying what you said.

I also tried to comment out the portion the remote device, but.............

the line

jclass discoveryListenerClass=jniEnv->GetObjectClass(discoveryListener);

did the same.

haven't you any other suggestions?

thanks.

minashokrya at 2007-7-11 23:21:36 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
thanks for all who viewed this forum.I discovered my problem, it was my mistake (one of my functions was detaching the thread from the JVM so my jniEnv pointer became invalid).
minashokrya at 2007-7-11 23:21:36 > top of Java-index,Java HotSpot Virtual Machine,Specifications...