32

Normally whats the reason to get java.lang.ClassCastException ..? I get the following error in my application

java.lang.ClassCastException: [Lcom.rsa.authagent.authapi.realmstat.AUTHw
krock
  • 28,904
  • 13
  • 79
  • 85
selvam
  • 501
  • 2
  • 5
  • 6
  • 1
    Looks like you don't have the full exception message there. Assuming you're using Java 5 or later the exception should include details of *what* can't be cast to *what* e.g. ` java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer` – mikej Aug 18 '10 at 10:45

6 Answers6

52

According to the documentation:

Thrown to indicate that the code has attempted to cast an Object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:

Object x = new Integer(0);
System.out.println((String)x); 
AndroidOptimist
  • 1,419
  • 3
  • 23
  • 38
Laurențiu Dascălu
  • 2,039
  • 3
  • 22
  • 23
  • 7
    +1 for documentaton. The documentation of exceptions usually gives a good hint at what could be wrong. – Joachim Sauer Aug 18 '10 at 10:46
  • +1 for documentation! If you don't feel like using google all the time you can download the source of the java library (or search if you already have src.zip somewhere in your java sdk folder). Then if you link it to your project you can just open the relevant file and read the javadocs there... – Michael Clerx Aug 18 '10 at 15:16
  • 1
    bine Laur, ce mic e internetul :) – ieugen Jul 24 '13 at 06:01
27

A ClassCastException ocurrs when you try to cast an instance of an Object to a type that it is not. Casting only works when the casted object follows an "is a" relationship to the type you are trying to cast to. For Example

Apple myApple = new Apple();
Fruit myFruit = (Fruit)myApple;

This works because an apple 'is a' fruit. However if we reverse this.

Fruit myFruit = new Fruit();
Apple myApple = (Apple)myFruit;

This will throw a ClasCastException because a Fruit is not (always) an Apple.

It is good practice to guard any explicit casts with an instanceof check first:

if (myApple instanceof Fruit) {
  Fruit myFruit = (Fruit)myApple;
}
NoseKnowsAll
  • 4,593
  • 2
  • 23
  • 44
mR_fr0g
  • 8,462
  • 7
  • 39
  • 54
  • If the last example changes to `if (myFruit instanceof Apple) { Apple myApple = (Apple) myFruit; }`, it'll be more illustrative – LittleLittleQ Jul 12 '16 at 03:23
6

@Laurențiu Dascălu's answer explains how / why you get a ClassCastException.

Your exception message looks rather suspicious to me, but it might help you to know that "[Lcom.rsa.authagent.authapi.realmstat.AUTHw" means that the actual type of the object that you were trying to cast was com.rsa.authagent.authapi.realmstat.AUTHw[]; i.e. it was an array object.

Normally, the next steps to solving a problem like this are:

  • examining the stacktrace to figure out which line of which class threw the exception,
  • examining the corresponding source code, to see what the expected type, and
  • tracing back to see where the object with the "wrong" type came from.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

It's because you're casting to the wrong thing - you're trying to convert to a particular type, and the object that your express refers to is incompatible with that type. For example:

Object x = "this is a string";
InputStream y = (InputStream) x; // This will throw ClassCastException

If you could provide a code sample, that would really help...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

To avoid x !instance of Long prob Add

<property name="openjpa.Compatibility" value="StrictIdentityValues=false"/>

in your persistence.xml

oers
  • 18,436
  • 13
  • 66
  • 75
0
ClassA a = <something>;
ClassB b = (ClassB) a;

The 2nd line will fail if ClassA is not a subclass of ClassB, and will throw a ClassCastException.

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79