Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting class cast exception where both classes are exactly the same

Tags:

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 related to the form which is shown on the screen, it doesn't make sense to me why is it the same class, it seems impossible.

like image 293
Phil Avatar asked Mar 03 '10 14:03

Phil


People also ask

How do you resolve ClassCastException?

// type cast an parent type to its child type. In order to deal with ClassCastException be careful that when you're trying to typecast an object of a class into another class ensure that the new type belongs to one of its parent classes or do not try to typecast a parent object to its child type.

Is it possible to load a class by two ClassLoader?

A class is always identified using its fully qualified name (package. classname). So when a class is loaded into JVM, you have an entry as (package, classname, classloader). Therefore the same class can be loaded twice by two different ClassLoader instances.

What causes ClassCastException?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

Can we catch ClassCastException?

You would get a ClassCastException, but not whithin the cast-method, but at the assignment to s. But this will still lead to an error later, when the you use your class.


2 Answers

This happens when two different ClassLoader objects load classes with the same name. The equality of two classes in Java depends on the fully qualified name and the class loader that loaded it.

So if two independent class loaders load classes from the same location, then objects of those types will not be able to be cast to each others type, even if their classes are called the same.

like image 117
Joachim Sauer Avatar answered Sep 22 '22 18:09

Joachim Sauer


As Joachim explained earlier, java.lang.ClassCastException typically occurs when two classloaders load the classes with the same name. However, i have come across another situation when this could occur.

This could occur with some IDE's that automatically reloads classes that have been modified. In such cases there might be older versions of the class retained in memory causing ClassCastException.

Here are a few ways you could resolve this issue :

  1. If you are writing a custom class loader, while loading a class make sure that the base/default class loader does not already have an instance of that class loaded.

  2. Make the class being loaded a sub-class of the class that is already loaded by the default class loader.

  3. Make the class being loaded implement an interface that is already loaded by the default class loader.

More info here - http://www.jspwiki.org/wiki/A2AClassCastException

like image 25
shaktimaan Avatar answered Sep 23 '22 18:09

shaktimaan