简体   繁体   English

java.math.BigInteger 不能转换为 java.lang.Integer

[英]java.math.BigInteger cannot be cast to java.lang.Integer

I'm getting the following exception.我收到以下异常。

Caused by:造成的:

java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Integer java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Integer

with the following code使用以下代码

List queryResult = query.list();

for (Iterator<Object[]> it = queryResult.iterator(); it.hasNext();) {
    Object[] result = it.next();
    Integer childId = (Integer) result[0];
    Integer grandChildCount = (Integer) result[1];
    CompanyNode childNode = childNodes.get(childId);
    childNode.setHasChildren(grandChildCount != 0);
    childNode.setIsLeaf(grandChildCount == 0);
}

at this line在这条线上

Integer grandChildCount = (Integer) result[1];

Does anybody have any idea?有人知道吗?

You can use: 您可以使用:

Integer grandChildCount = ((BigInteger) result[1]).intValue();

Or perhaps cast to Number to cover both Integer and BigInteger values. 或者可能强制转换为Number以覆盖IntegerBigInteger值。

As we see from the javaDoc, BigInteger is not a subclass of Integer : 正如我们从javaDoc看到的那样, BigInteger不是Integer的子类:

java.lang.Object                      java.lang.Object
   java.lang.Number                       java.lang.Number
      java.math.BigInteger                    java.lang.Integer

And that's the reason why casting from BigInteger to Integer is impossible. 这就是为什么从BigIntegerInteger是不可能的原因。

Casting of java primitives will do some conversion (like casting from double to int ) while casting of types will never transform classes. 抛出java原语会做一些转换(比如从double转换为int ),而类型的转换永远不会转换类。

java.lang.Integer is not a super class of BigInteger . java.lang.Integer不是BigInteger的超类。 Both BigInteger and Integer do inherit from java.lang.Number , so you could cast to a java.lang.Number . BigIntegerInteger都继承自java.lang.Number ,因此您可以转换为java.lang.Number

See the java docs http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Number.html 请参阅java docs http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Number.html

The column in the database is probably a DECIMAL . 数据库中的列可能是DECIMAL You should process it as a BigInteger , not an Integer , otherwise you are losing digits. 您应该将其作为BigInteger ,而不是Integer ,否则您将丢失数字。 Or else change the column to int . 或者将列更改为int

You can try this: 你可以试试这个:

((BigDecimal) volume).intValue();

I use java.math.BigDecimal convert to int (primitive type). 我使用java.math.BigDecimal转换为int (原始类型)。

It is worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.Integer不能强制转换为java.math.BigInteger - java.lang.Integer cannot be cast to java.math.BigInteger 如何解决 java.math.BigInteger 无法转换为 java.lang.Integer? - How to work around java.math.BigInteger cannot be cast to java.lang.Integer? 如何解决 java.math.BigInteger 无法转换为 java.lang.Integer - How to work around java.math.BigInteger cannot be cast to java.lang.Integer Playorm java.lang.Integer无法强制转换为java.math.BigInteger - Playorm java.lang.Integer cannot be cast to java.math.BigInteger java.math.BigInteger 不能强制转换为 java.lang.Integer,ZAC5C24B64B4BAC83 - java.math.BigInteger cannot be cast to java.lang.Integer , sql Dialect is not configured java.lang.ClassCastException:无法将java.lang.Integer强制转换为java.math.BigInteger HTTP状态500 - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.math.BigInteger HTTP Status 500 java.math.BigInteger 无法转换为 java.lang.Long - java.math.BigInteger cannot be cast to java.lang.Long java.lang.Long无法强制转换为java.math.BigInteger - java.lang.Long cannot be cast to java.math.BigInteger java.lang.ClassCastException:java.math.BigInteger无法强制转换为java.lang.Long - java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long java.math.BigInteger无法强制转换为java.math.BigDecimal - java.math.BigInteger cannot be cast to java.math.BigDecimal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM