2014-02-13 3 views
1

메서드에서 다음 코드가있는 줄에 오류가 발생했음을 기억합니다.ClassCastException 및 호환되지 않음

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

Integer count = (Integer) result[1]; 

에서

나는

오류가 간
Integer count = ((BigDecimal) result[1]).intValue(); 

에 코드를 교체했다.

하지만 일부 새 프로젝트에서 동일한 코드 조각에 대해 다음과 같은 오류가 발생했습니다.

두 가지 모두 동일하거나 동일하지 않습니다.

+0

그것은 당신이 같은 코드에 대해 동일한 오류가 발생했습니다 경우보다 더 자주는 아니지만, ... result'이 무엇인지 '모르고 –

+4

당신을 돕기 위해 매우 어렵습니다,이 같은 문제이고 동일한 해결책이 적용될 수 있습니다. – SudoRahul

+0

결과 배열의 유형은 무엇입니까? – Sanjeev

답변

0

둘 다 동일하지 않음 Integer count = (Integer) result[1];은 BigDecimal Object를 정수로 변환하려고 시도합니다. 항상 예외가 될 것입니다.

Integer count = ((BigDecimal) result[1]).intValue(); 결과에 BigDecimal 개체가있는 경우 작동합니다.

코드를 올바르게 컴파일했는지 확인하는 것이 좋습니다.

Integer count = ((BigDecimal) result[1]).intValueExact(); 

또는 :

0

BigDecimal.intValueExact() (또는 BigDecimal.intValue을()) :

Converts this BigDecimal to an int. This conversion is analogous to a narrowing primitive conversion from double to short as defined in the Java Language Specification: any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in an int, only the low-order 32 bits are returned. Note that this conversion can lose information about the overall magnitude and precision of this BigDecimal value as well as return a result with the opposite sign.

Plase이 BigDecimal를 포함 할 수 고려 Integer.MAX_VALUE보다 큰 값

참조 : here

관련 문제