When I did some testing codes , I got some effective answers, but I found some other problems(or just I think is).
Codes like:
JNIEXPORT jint JNICALL test_jni_Native_testSet(JNIEnv *env, jclass type, jobject tdobj)
{
//Create Integer class, get constructor and create Integer object
jclass intClass = env->FindClass(env, "java/lang/Integer");
jmethodID initInt = env->GetMethodID(env, intClass, "<init>", "(I)V");
if (NULL == initInt) return -1;
jobject newIntObj = env->NewObject(env, intClass, initInt, 123);
//Now set your integer into value atttribute. For this, I would
//recommend you to have a java setter and call it in the same way
//as shown above
//clean reference
env->DeleteLocalRef(env, newIntObj);
return 0;
}
If I replace 123 with NULL in row
jobject newIntObj = env->NewObject(env, intClass, initInt, 123);
In java codes, I set a Integer parameter by this native function. It seems like that env->NewObject won't return NULL, if it calls the constructor successfully. It means that I can only assign a jobject with NULL rather than calling NewObject function and initializing it will NULL.
I don't think it a good handl.
As http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html said:
env->NewObject(...)
RETURNS:
Returns a Java object, or NULL if the object cannot be constructed.
So, my question is :
Why not return NULL when we initializing a object by NULL value?
The function NewObject
uses a non-type safe, variable argument list. If you pass NULL to the constructor, it will be interpreted as the integer value 0. Therefore, it will successfully construct an Integer object initialized to 0.