Cannot cast class java.lang.Integer to class java.lang.String

Madhumathi :

    public int compareTo(ListItem item) {
        if(item!=null) {
            return ((String) super.getValue()).compareTo((String) item.getValue());
        }

My program is based on the Abstract class concept.

How can I resolve this java.lang.ClassCastException: in my program?

Jordan :

Casting in Java doesn't magically convert data from one type to another. Rather, it tells Java that the object stored in a variable actually is some other type, which is useful when dealing with inheritance or interfaces. See this StackOverflow question for more details.

In this case, the value you get from item.getValue() is an Integer. Trying to cast to a String does not, and can not, work, because it's not a String. It really is an Integer, and trying to call String methods on it won't work.

As others have mentioned, you can convert the Integer to a String using Integer.toString(someInteger). However, since you're doing a comparison, you should probably just compare the values as Integers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

java.lang.Integer cannot be cast to java.lang.Long

Class cannot be cast to java.lang.Comparable

java.lang.ClassCastException: CLASS/Activity cannot be cast to MainActivity

java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class

java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

[java.lang.String; cannot be cast to java.lang.String

java.lang.ClassCastException: [Z cannot be cast to [Ljava.lang.String in reflective toString on model class

Getting java.lang.String cannot be cast to java.lang.Integer when retrieving value from SharedPreferences

Getting a ClassCastException: java.lang.Integer cannot be cast to java.lang.String error without casting

sharedPref.getInt: java.lang.String cannot be cast to java.lang.Integer

how to resolve java.lang.string cannot be cast to java.lang.integer android with xml parser

Cannot cast object 'url' with class 'java.lang.String' to class 'int' Gradle Java

java.lang.Long cannot be cast to java.lang.Integer

Java.lang.ClassCastException: model class cannot cast to model class

How to fix "class java.lang.Long cannot be cast to class java.lang.Integer" error in Java, when i want return result list

MongoDB: class java.lang.Double cannot be cast to class java.lang.Long

Collections.sort method is throwing class java.lang.Integer cannot be cast to class java.lang.Double error

java.lang.ClassCastException: class java.sql.Date cannot be cast to java.lang.String

Mockito java.lang.ClassCastException: my.custom.class.Project cannot be cast to java.lang.Boolean

Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

java.lang.Integer cannot be cast to java.lang.String

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer in tableau?

java.lang.ClassCastException (Cannot cast class to same class)

class java.lang.Long cannot be cast to class java.lang.String error when having a hashmap of mixed types (Long and string)

java.lang.Class cannot be cast to clojure.lang.IFn when date capturing and string formatting

Why get class cannot be cast to class java.lang.Comparable

Class cannot be cast to class java.lang.reflect.ParameterizedType (java.lang.Class and java.lang.reflect.ParameterizedType )

Exception in main java.lang.ClassCastException:class java.lang.String can't be cast to class

java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Integer; Concatenate/Merge/join two Integer[] Array