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?
1609 questions
13
votes
3 answers

java.lang.ClassCastException in Grails app

I am following the free online book "Getting Started with Grails" (http://www.infoq.com/minibooks/grails-getting-started) and I am getting a java.lang.ClassCastException when trying to list any domain class. Can anyone decipher this? URI:…
grantmcconnaughey
  • 8,774
  • 8
  • 33
  • 62
13
votes
1 answer

ClassCastException: java.lang.Object cannot be cast to java.lang.Integer

The root of my problem is that I have a method that handles JDBC queries and releases all connections after the query. A "ResultSet" is passed back to the calling method. I have found that I can't simply pass the ResultSet back to the calling…
Lurk21
  • 2,198
  • 11
  • 34
  • 52
12
votes
10 answers

"Iterable cannot be cast to List" - Isn't `List` a type of `Iterable`?

I called a getElements method which returns Iterable. I did this: List elements = (List) getElements(); This generates the error: java.lang.ClassCastException: com.utesy.Element$3 cannot be cast to java.util.List I…
snoopy
  • 583
  • 2
  • 5
  • 12
12
votes
9 answers

android classcastexception on activity startup

i have a simple activity program in android. basically the class just extends Activity. But when i start it i get a ClassCastException in the constructor of my class. i dont even have a constructor defined, so it must be in the constructor of the…
clamp
  • 30,396
  • 73
  • 193
  • 291
12
votes
3 answers

Spark 1.6.0 throwing classcast exception in Cluster Mode works fine in local mode

I am trying simple word-count example on spark in Cluster Mode and Local Mode it works fine in local mode but throwing class cast exception in Cluster Mode here is code snippet... package com.example import com.typesafe.config.ConfigFactory import…
Rahul Shukla
  • 495
  • 6
  • 20
11
votes
5 answers

readResolve not working ? : an instance of Guava's SerializedForm appears

During deserialization of one of our data structure (using the default mechanism (no custom writeObject/readObject)), an instance of ImmutableMap$SerializedForm (from google's Guava library) shows up. Such an instance should not be visible from…
Clément Hurlin
  • 390
  • 2
  • 10
11
votes
3 answers

Hibernate 4 ClassCastException on LAZY loading while EAGER works fine

I have the following JOINED inheritance root entity for geographic areas (like continents, countries, states etc.): @Entity @Table(name = "GeoAreas") @Inheritance(strategy = InheritanceType.JOINED) public abstract class GeoArea implements…
Kawu
  • 12,601
  • 31
  • 109
  • 189
11
votes
3 answers

How do you disable a button inside of an AlertDialog?

I am trying to write an AlertDialog with 3 buttons. I want the middle, Neutral Button to be disabled if a certain condition is not met. Code int playerint = settings.getPlayerInt(); int monsterint = settings.getMonsterInt(); …
Frank Bozzo
  • 13,873
  • 6
  • 22
  • 29
11
votes
1 answer

How to fix 'class java.util.LinkedHashMap cannot be cast to class' when using Java 8 and Java 11

I'm working with a micro-service built using Java 11, this service has a dependency built in Java 8. Dependency has rest-clients in it and there is a method that does this: public ResponseEntity> getForList(String url,…
Alex Andrade
  • 182
  • 1
  • 1
  • 13
11
votes
2 answers

How to detect ambiguous method calls that would cause a ClassCastException in Java 8?

We are currently in the process of migrating an application from Java 7 to Java 8. After fixing a some compilation issues, I stumbled upon an issue similar to the following question: ClassCast Error: Java 7 vs Java 8. To summarise, here is a sample…
Didier L
  • 13,411
  • 5
  • 46
  • 90
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
  • 1
  • 7
  • 14
11
votes
3 answers

MyClass cannot be cast to java.lang.Comparable: java.lang.ClassCastException

I am doing a java project and I got this problem and don't know how to fix it. The classes in my project (simplified): public class Item { private String itemID; private Integer price; public Integer getPrice() { return…
user2234225
  • 119
  • 1
  • 1
  • 4
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
  • 7,361
  • 3
  • 36
  • 54
10
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,568
  • 4
  • 19
  • 29
10
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 =…