Tag Archives: Encapsulation class Integer

Regarding [java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Boolean], combined with examples

Description:
I use the Groovy language, but it is quite Java compatible, except that it is a weakly typed language relative to Java
Situation:
If a string is null, no unit will be added. The screenshot is shown below, and the data after “Distance (m) :” should be intercepted

Original script:

 if( !distance.endsWith(")") && !distance.endsWith("m") && distance != null && distance.length() != 0) {
    distance = distance + 'm';
}

Error: Collection exception. Exception information [Java. Lang. ClassCastException: Java. Lang. Integer always be cast to Java. Lang. Boolean]
Later changed the script:

if(!distance.endsWith(")") && !distance.endsWith("m") && distance != null && distance.toString().length() != 0) {
    distance = distance + 'm';
}

Exception resolution. Integer is int the wrapper class, there is no length () this method, the specific introduction, see this post:
https://blog.csdn.net/andyzhaojianhui/article/details/84324466