对于我的项目,我正在使用Java Native Interface(JNI)。
我的项目包含一个JNI系统,该系统使用port参数调用de C ++端的函数。这个函数比返回一个Object数组,如下面的注释中所写,问题在最后的评论中说明:
public Integer SpawnNewConnectionHandler(Integer Port) throws WrapperException {
//Creation of the object array that will hold the result
Object[] result;
//Get the result object array from the external library. Length of the array is either 1 or 2.
//While debugging i found out that it contains two elements of the type int (Not integer, but its basetype).
//This means that nothing on the c++ side went wrong.
result = spawnNewServerConnectionHandler(Port);
//Error coming from the SDK should be handled here. (Code snipped as the if clause checking for it, is not being called.)
//The snipped will throw a WrapperException if the length of the array returned is not 1.
//The debugger will break here and show me that before executing this line both of the result objects are not empty,
//So result[0] and result[1] are filled with data, for result[1] it is a 1 or higher. For result[0] is is the number 0. (Returned as a fact that everything went alright while executing.)
//During execution of this line the program crashes and shows a NullPointerException.
//
//I found out that it is impossible for it to cast the object, stored in the object array as a int into a Integer.
//Doing this manually while debugging shows the following error message:
//"Unable to cast numeric value to java.lang.Integer."
return (Integer) result[1];
}
有人知道这个问题的解决方案,或者至少为什么它会给我这个奇怪的错误?
答案 0 :(得分:1)
转换不会更改对象的类型。为了将结果[1]强制转换为整数,结果[1]必须已经是一个整数。
您没有说明数组中的对象类型。我知道数组被声明为Object,但这意味着数组中的对象可以是任何对象,因为任何对象都可以是对象的子类。
您说数组中的值具有值,但您没有说明您是如何知道这些值或您拥有的值。您排序的错误消息与您的解释并不完全匹配。所以很难确切地说出发生了什么。但是你没有要转换的Integer对象,这就是演员无效的原因。
答案 1 :(得分:1)
根据您对@rcook的评论,您似乎有一个int[]
数组,而不是Integer[]
。这里的区别在于int
是primitive type而Integer
是对象类型。
试试这个:
public Integer SpawnNewConnectionHandler(Integer Port) throws WrapperException {
//Creation of the object array that will hold the result
int[] result;
//Get the result object array from the external library. Length of the array is either 1 or 2.
//While debugging i found out that it contains two elements of the type int (Not integer, but its basetype).
//This means that nothing on the c++ side went wrong.
result = (int[]) spawnNewServerConnectionHandler(Port);
//Error coming from the SDK should be handled here. (Code snipped as the if clause checking for it, is not being called.)
//The snipped will throw a WrapperException if the length of the array returned is not 1.
//The debugger will break here and show me that before executing this line both of the result objects are not empty,
//So result[0] and result[1] are filled with data, for result[1] it is a 1 or higher. For result[0] is is the number 0. (Returned as a fact that everything went alright while executing.)
//During execution of this line the program crashes and shows a NullPointerException.
//
//I found out that it is impossible for it to cast the object, stored in the object array as a int into a Integer.
//Doing this manually while debugging shows the following error message:
//"Unable to cast numeric value to java.lang.Integer."
return Integer.valueOf(result[1]);
}
请注意,我已将所有对Object[]
的引用替换为int[]
。