I'm getting the following exception.
Caused by:
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?
The java. math. BigInteger. intValue() converts this BigInteger to an integer value.
BigInteger. intValue() converts this BigInteger to an int. This conversion is analogous to a narrowing primitive conversion from long to int.
BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java. lang. Math. It also provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.
You can use:
Integer grandChildCount = ((BigInteger) result[1]).intValue();
Or perhaps cast to Number
to cover both Integer
and BigInteger
values.
As we see from the javaDoc, BigInteger
is not a subclass of 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.
Casting of java primitives will do some conversion (like casting from double
to int
) while casting of types will never transform classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With