Questions tagged [classcastexception]

The Java exception thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

ClassCastException is the Java unchecked exception thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

FAQ:

  1. Explanation of "ClassCastException" in Java
  2. What else can throw a ClassCastException in java?
1698 questions
11
votes
3 answers

programmatically change layout height, ClassCastException?

I'm trying to "animate" a WebView to drop down and reveal its contents. I've written a handler to increase the height by 1 each time, however, I'm running into a ClassCastException. The code I'm using is: WebView.LayoutParams params = new…
Seth Nelson
  • 2,598
  • 4
  • 22
  • 29
11
votes
4 answers

Returning result of count native query using EntityManager in Java?

I have the following SQL Query : SELECT COUNT(*) FROM DOG where ID = 'SampleId'; I am trying to write this in java : public int returnCountOfDogTable(String id){ String sql= "SELECT COUNT(*) FROM DOG WHERE ID =:id"; Query query =…
java123999
  • 6,974
  • 36
  • 77
  • 121
11
votes
2 answers

Access Array column in Spark

A Spark DataFrame contains a column of type Array[Double]. It throw a ClassCastException exception when I try to get it back in a map() function. The following Scala code generate an exception. case class Dummy( x:Array[Double] ) val df =…
Boris
  • 1,093
  • 2
  • 14
  • 22
11
votes
3 answers

List to TreeSet conversion produces: "java.lang.ClassCastException: MyClass cannot be cast to java.lang.Comparable"

List myclassList = (List) rs.get(); TreeSet myclassSet = new TreeSet(myclassList); I don't understand why this code generates this: java.lang.ClassCastException: MyClass cannot be cast to…
Chuck
  • 685
  • 2
  • 7
  • 14
11
votes
3 answers

Get All Installed Application icons in Android : java.lang.ClassCastException

There is a java.lang.ClassCastException when I am trying to get a installed applications icon. Here is my code. public Bitmap getAppIcon(String path) { PackageInfo pi = pm.getPackageArchiveInfo(path, 0); pi.applicationInfo.sourceDir…
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
11
votes
3 answers

Reflections IllegalArgumentException causes

UPDATE - To make the question clearer. What is the possible cause of getting a ClassCastException while calling a method via reflections? I got the following stacktrace as a part of my application while trying to invoke a method via…
Aditya
  • 2,148
  • 3
  • 21
  • 34
10
votes
3 answers

ClassCastException while using varargs and generics

I'm using java generics and varargs. If I use the following code, I'll get a ClassCastException, even though I'm not using casts at all. Stranger yet, if I run this on Android (dalvik) no stack trace is included with the exception, and if I change…
murgo
  • 344
  • 3
  • 11
10
votes
1 answer

ClassCastException when stopping Tomcat 10 inside Eclipse

I am using Eclipse 2022-06 and Tomcat 10.0.10. Often, when shutting down Tomcat running inside Eclipse, I get WARNUNG: Failed to clear soft references from ObjectStreamClass$Caches for web application [ROOT] java.lang.ClassCastException: class…
Tim Lammarsch
  • 161
  • 1
  • 1
  • 10
10
votes
2 answers

Hibernate -> ArrayList cannot be cast to Set

I have a Java EE application and I use Hibernate. The domain objects, I changed the List / ArrayList to Set / HashSet, because it is better to use Sets. But in my Dao implementation I run into a problem: public Set getAllPersons() { …
Tim
  • 13,228
  • 36
  • 108
  • 159
10
votes
2 answers

No compiler error about incompatible casts

Sorry if this was already explained, but i didn't find similar threads anywhere in web. Today I opened one project class in IDE and saw an error (red underline), though project was compiled successfully. So, the code is: public interface…
AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
10
votes
3 answers

Why does it compile when casting to an unrelated interface?

interface Printable {} class BlackInk {} public class Main { public static void main(String args[]) { Printable printable = null; BlackInk blackInk = new BlackInk(); printable = (Printable)blackInk; } } If the…
korshyadoo
  • 337
  • 7
  • 18
10
votes
3 answers

Fatal Exception: String can't be cast to Spannable

My app works greatly with the exception of a few devices. On one such device, I get a FATAL EXCEPTION in one of my activities. The error is java.lang.ClassCastException: java.lang.String cannot be cast to android.text.Spannable ... ... at…
user1857437
  • 279
  • 4
  • 14
10
votes
3 answers

ClassCastException LinearLayout LayoutParams

I got an Actionbar and some Fragments. The Problem is my Homescreen. There is an overview Fragment on the left side and a detail Fragment on the right side. My TabListener is Dynamic so i want to set the weight of my Layout programatically. Whenever…
9
votes
2 answers

Changed order in main.xml, now I get ClassCastException

Using eclipse and the Android SDK, I managed a simple test app with a Button and a ProgressBar. All runs fine, except I did't want the ProgressBar to move the Button, when the ProgressBar was made visible, so just for testing I changed the order…
OppfinnarJocke
  • 1,038
  • 2
  • 9
  • 21
9
votes
6 answers

ClassCastException when casting looked-up EJB view in AS7

I have am deploying 2 EARs onto JBoss AS 7.1.0.Alpha1-SNAPSHOT (post 7.0.1.Final version). Both deploy fine. I have an EJB Singleton class packaged within a JAR, within one of the EARs: @Startup @Singleton // one of @Local(Store.class),…
Ben Kirby
  • 904
  • 2
  • 11
  • 29