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
14
votes
1 answer

How to properly configure Jakarta EE libraries in Maven pom.xml for Tomcat?

I have been trying in vain to implement tomcat 9's jakarta servlet as opposed to the previous javax.servlet implementation (as its my understanding that the jakarta package is the way forward). The issue is that when I point the browser at my…
user14998757
  • 161
  • 1
  • 1
  • 6
14
votes
4 answers

No ClassCastException is thrown inside Java generics

Below is the first Java generics I've ever written : public class MyClass { public static T castToAnotherType(Object param) { T ret = null; try { ret = (T) param; } catch (ClassCastException e) { …
Livy
  • 631
  • 4
  • 15
14
votes
5 answers

No ClassCastException when casting to generic type different to actual class

I have some code which looks something like this (part of a negative test of the method get): import java.util.*; public class Test { Map map = new HashMap<>(); public static void main (String ... args) { Test test =…
RobEarl
  • 7,862
  • 6
  • 35
  • 50
14
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
  • 149
  • 1
  • 1
  • 4
13
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
  • 13,647
  • 34
  • 123
  • 195
13
votes
3 answers

cannot cast to java.lang.Comparable

Although this question has already been asked but I have an implementation specific doubt. I am trying to print the top view of the binary tree and following is the complete code for it: import java.util.*; class Node{ int data; Node…
pkenil96
  • 173
  • 1
  • 1
  • 7
13
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
  • 603
  • 2
  • 6
  • 12
13
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
  • 18,905
  • 10
  • 61
  • 103
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
  • 10,130
  • 10
  • 37
  • 66
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,307
  • 12
  • 37
  • 55
12
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
  • 440
  • 3
  • 11
12
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
  • 15,033
  • 6
  • 25
  • 29
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
  • 33,000
  • 75
  • 203
  • 299
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
  • 505
  • 7
  • 20
12
votes
1 answer

How to fix "java.lang.ClassCastException: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to android.app.Activity" exception

When I create a custom View, where in some cases I need to cast the Context class passed via the constructor for the Activity class, for making some tasks like inflate a View directly inside my custom View class, I'm getting the following…
Jorge Gil
  • 4,265
  • 5
  • 38
  • 57