【问题标题】:Problems with int object to integer castingint 对象到整数转换的问题
【发布时间】:2014-02-26 02:02:12
【问题描述】:

对于我的一个项目,我正在使用 Java 本机接口 (JNI)。

我的项目包含一个 JNI 系统,它使用端口参数在 de C++ 端调用一个函数。此函数返回一个 Object 数组,如下面的 cmets 中所写,问题在最后一条评论中说明:

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];
}

有没有人知道这个问题的解决方案,或者至少为什么它会给我这个奇怪的错误?

【问题讨论】:

  • 您是否尝试过将Object[] result 声明为int[] result?我怀疑你有一个原始数组,在这种情况下你的返回看起来像Integer.valueOf(result[1])
  • sigpwned,我确实有一个包含由外部库返回的整数的 ObjectArray。如果我尝试强制转换,则会在编译时给我一个错误,并且它说这两种类型不兼容。
  • int[]Integer[] 数组之间存在差异。即,第一个是原始类型,第二个是对象类型。根据您在下面对{int[2]@2559} 的评论,很明显您手上有一个原始数组。稍等一下——我会用一些我认为有用的代码来回应。
  • 是的,我知道,但是转换无法正常工作,编译器抛出一个错误,抱怨不兼容的类型,期待 int[] found java.lang.object[]。

标签: java nullpointerexception java-native-interface integer int


【解决方案1】:

铸造不会改变对象的类型。为了将 result[1] 转换为 Integer,result[1] 必须已经是整数。

你没有说数组中有什么类型的对象。我知道数组被声明为 Object,但这意味着数组中的对象可以是任何东西,因为任何对象都可以是对象的子类。

你说数组中的值有值,但你没有说你是怎么知道的或者你有什么样的值。您给出的错误消息与您的解释并不完全相符。所以很难说到底发生了什么。但是您没有要转换的 Integer 对象,这就是转换不起作用的原因。

【讨论】:

  • 在调试时,我在执行返回行之前停止了代码,它说 result = {int[2]@2559} (最后四个数字改变,因为它是内存位置)。
  • 我同意这似乎是一个 int 数组——我认为你可以返回 "new Integer(result[1]);"
【解决方案2】:

根据您对@rcook 的评论,您似乎有一个int[] 数组,而不是Integer[]。这里的区别在于intprimitive typeInteger 是对象类型。

试试这个:

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[]

【讨论】:

  • 我试过了,它给出了一个不兼容的类型错误;预期的 int[] 在我尝试编译时找到了 java.lang.object[]。
  • 您是否为spawnNewServerConnectionHandler() 编写了JNI 声明?如果是这样,您可能需要将声明返回类型更改为int[]
  • 是的,我使用 javah 编写了 JNI 声明。如果我让它返回一个 jIntArray,我知道它会起作用。但从长远来看,这并不能解决问题。我需要在此之后调用的第二个 JNI 函数同时返回一个字符串和一个整数。这意味着我需要使用对象数组而不是 int[]。所以我想为什么不尝试实现一个通用方法并为两者使用相同的系统。
  • 这与您发布的问题截然不同!您发布的代码不起作用,因为您试图将int[] 视为Integer[]。如果您需要知道如何在 JNI 中创建 Object[]Integer,您可能会发现 this question 很有用。但是,您最好返回一个包含Stringint 而不是Object[] 的自定义对象。
  • 真的可以用 JNI 做这样的事情吗,我的意思是使用自定义对象?嗯,这实际上是一个不错的概念。我会看看它谢谢你的提示!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 2020-07-31
  • 2012-10-17
  • 2023-01-26
  • 1970-01-01
相关资源
最近更新 更多