Reputation: 9
For a project of mine i am working with the Java Native Interface (JNI).
My project contains a JNI System that calls a function on de C++ side with a port parameter. This function than returns a Object array as written in the comments below, the problem is stated in the last comment:
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];
}
Does anybody know a solution to this problem, or at least why it is giving me this weird error?
Upvotes: 0
Views: 1422
Reputation: 7453
Based on your comment to @rcook, it appears that you have an int[]
array, not an Integer[]
. The difference here is that int
is a primitive type and Integer
is an object type.
Try this:
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]);
}
Note that I've replaced all references to Object[]
with int[]
.
Upvotes: 1
Reputation: 13143
Casting does not change the type of the object. In order to cast result[1] to Integer, result[1] must BE an integer already.
You do not say what type of objects are in the array. I know the array is declared Object, but that means that the objects in the array could be anything, since any object can be a subclass of object.
You say the values in the arrays have values, but you do not say how you know that or what kind of values you have. The error message you sort-of give doesn't really match your sort-of explanation. So it's hard to say exactly what is going on. But you don't have an Integer object to cast, that's why the cast doesn't work.
Upvotes: 1