[英]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
以覆盖Integer
和BigInteger
值。
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. 这就是为什么从
BigInteger
为Integer
是不可能的原因。
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
. BigInteger
和Integer
都继承自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.