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
166
votes
7 answers

explicit casting from super class to subclass

public class Animal { public void eat() {} } public class Dog extends Animal { public void eat() {} public void main(String[] args) { Animal animal = new Animal(); Dog dog = (Dog) animal; } } The assignment Dog dog…
saravanan
  • 5,019
  • 7
  • 40
  • 51
110
votes
4 answers

java: (String[])List.toArray() gives ClassCastException

The following code (run in android) always gives me a ClassCastException in the 3rd line: final String[] v1 = i18nCategory.translation.get(id); final ArrayList v2 = new ArrayList(Arrays.asList(v1)); String[] v3 =…
Gavriel
  • 18,088
  • 12
  • 63
  • 98
80
votes
12 answers

Explanation of ClassCastException in Java

I read some articles written on "ClassCastException", but I couldn't get a good idea on what it means. What is a ClassCastException?
Chathuranga Chandrasekara
  • 19,150
  • 28
  • 95
  • 135
59
votes
4 answers

What else can throw a ClassCastException in java?

This is an interview question. The interview is over, but this question is still on my mind. I can't ask the interviewer, as I did not get the job. Scenario: put object of class C1 in to a cache with key "a" Later code: C1 c1FromCache = (C1)…
tgkprog
  • 4,405
  • 4
  • 39
  • 66
58
votes
11 answers

ClassCastException when casting to the same class

I have 2 different Java projects, one has 2 classes: dynamicbeans.DynamicBean2 and dynamic.Validator. On the other project, I load both of these classes dynamically and store them on an Object class Form { Class beanClass; Class…
Jaime Garcia
  • 5,894
  • 7
  • 47
  • 60
54
votes
4 answers

Why am I getting a ClassCastException when generating javadocs?

I'm using ant to generate javadocs, but get this exception over and over - why? I'm using JDK version 1.6.0_06. [javadoc] java.lang.ClassCastException: com.sun.tools.javadoc.ClassDocImpl cannot be cast to com.sun.javadoc.AnnotationTypeDoc …
ScArcher2
  • 78,317
  • 42
  • 111
  • 158
45
votes
10 answers

ClassCastException, casting Integer to Double

ArrayList marks = new ArrayList(); Double sum = 0.0; sum = ((Double)marks.get(i)); Everytime I try to run my program, I get a ClassCastException that states: java.lang.Integer cannot be cast to java.lang.Double
Khuba
  • 451
  • 1
  • 4
  • 3
44
votes
4 answers

Android: ClassCastException when adding a header view to ExpandableListView

I'm trying to add a header to an ExpandableListView like so: headerView = View.inflate(this, R.layout.header, null); expandableListView.addHeaderView(headerView); expandableListView.setAdapter(new SectionedAdapter(this)); Which gives me the…
43
votes
6 answers

android.app.Application cannot be cast to android.app.Activity

I'm trying to change a LinearLayout from another class, but when i run this code: public class IRC extends PircBot { ArrayList channels; ArrayList userCount; ArrayList topics; LinearLayout channelLayout; Context…
DomeWTF
  • 2,134
  • 4
  • 29
  • 42
41
votes
2 answers

can't cast to implemented interface

i'm very confused... I have a class which directly implements an interface: public class Device implements AutocompleteResult {...} Here is proof that I'm looking at the right variables: Object match = ...; log.debug(match.getClass()); // Outputs…
pstanton
  • 29,804
  • 23
  • 108
  • 165
41
votes
1 answer

ClassCastException: RestTemplate returning List instead of List

I'm trying to access getter methods on my MyModelClass but my code is returning List instead of List. This is my code. List myModelClass=(List)…
Erick
  • 615
  • 1
  • 7
  • 15
28
votes
3 answers

How does one declare the type of an Android preference?

I have a preferences.xml that looks like this:
David R.
  • 483
  • 1
  • 6
  • 10
27
votes
3 answers

Casting Java functional interfaces

As always I was looking through JDK 8 sources and found very interesting code: @Override default void forEachRemaining(Consumer action) { if (action instanceof IntConsumer) { forEachRemaining((IntConsumer) action); }…
Andrii Abramov
  • 7,967
  • 8
  • 55
  • 79
25
votes
5 answers

Getting class cast exception where both classes are exactly the same

I am doing a JBoss SEAM project and when I view a form I get this error. java.lang.ClassCastException: it.cogitoweb.csi.entity.csiorelav.CsiTipoLav cannot be cast to it.cogitoweb.csi.entity.csiorelav.CsiTipoLav Its alway the same JPA class which is…
Phil
  • 32,917
  • 29
  • 99
  • 154
21
votes
4 answers

java.lang.ClassCastException: com.sun.faces.facelets.compiler.UIInstructions cannot be cast to org.primefaces.model.menu.MenuElement

This morning I came in to my office and the first thing I saw: my company site crashed. Because of me I think. Now I tried for one hour and a half to fix this, but I found nothing and everybody looses their mind because the website doesn't work and…
user1713847
1
2 3
99 100