Java将null传递给C/C++代码是很容易出现问题,那么如何处理呢?看下面这个例子:
在C/C 代码中判断传递过来的对象是否为NULL
class JNIString
{
static
{
System.loadLibrary("JNIString");
}
public static native String strMethod(String s);
}
/*
javac JNIString.java
javah JNIString.java// get the file JNIString.h
*/
#include "JNIString.h"
#include
JNIEXPORT jstring JNICALL Java_JNIString_strMethod(JNIEnv *env, jclass jcls, jstring jstr)
{
int i;
char str2[128];
if(jstr == NULL)// 判断传递过来的对象是否为NULL
{
printf("null\n");
return NULL;// 此外,也可以返回NULL,Java得到的将是null
}
const char *str = env->GetStringUTFChars(jstr, NULL);// must be const
// const jchar * GetStringChars(JNIEnv *