Solved

how to fix "The exception was: java.lang.ClassCastException" error?

Posted on 2003-11-18
17
488 Views
Last Modified: 2012-05-04
Hi experts,

I have some codes work on one latest verions of jdk and broken on older version.

The error is "The exception was: java.lang.ClassCastException", which I just can't see why it won't work. Is there a way to find out exactly what class the object is? So, I can compare with what it is cast to? thanks.
0
Comment
Question by:panpioneer
  • 6
  • 4
  • 3
  • +1
17 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 25 total points
ID: 9771718
> Is there a way to find out exactly what class the object is?

if( o instanceof String )
{
    String s = (String)o ;
}

0
 
LVL 35

Expert Comment

by:TimYates
ID: 9771723
you can also do:

System.out.println( o.getClass.getName() ) ;

to see what it is...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9771725
Sorry...

System.out.println( o.getClass().getName() ) ;
0
 

Author Comment

by:panpioneer
ID: 9771962

THanks. It is very strange that both object are of the same type after the testing, however, when I assign one object to another, like the following, then I got the "java.lang.ClassCastException" error. why?

*****************************************
Research ac = new Research();
ac = (Research)request.getAttribute("ab");      

System.out.println((request.getAttribute("ab")).getClass().getName() ) ;
System.out.println(ac.getClass().getName() ) ;       
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9772152
I'm not sure why if this:

>>System.out.println((request.getAttribute("ab")).getClass().getName() ) ;

is printing Research (in whatever package)

But there is a redundancy in your code:

>>Research ac = new Research();

creates a new instance. You should be doing:

Research ac = null;
try {
      Object o = request.getAttribute("ab");
      ac = (Research)o;
}
catch(ClassCastException e) {
      System.err.println("Object was of type " + o.getClass().getName());
}

0
 

Author Comment

by:panpioneer
ID: 9772303
thanks. But, if I copy your code, I will get another error message: o is not defined....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9772315
Sorry!

Research ac = null;
Object o = null;
try {
     o = request.getAttribute("ab");
     ac = (Research)o;
}
catch(ClassCastException e) {
     System.err.println("Object was of type " + o.getClass().getName());
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:panpioneer
ID: 9772321
CHEJ,

one more thing, if i move

Object o = request.getAttribute("ab"); to out side of try block, I am getting java.lang.null.exception.
0
 

Author Comment

by:panpioneer
ID: 9772324

Oop! it is java.lang.NullPointerException
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9772361
Always test for null:

if (ac != null) {
   // you got it
}
0
 

Author Comment

by:panpioneer
ID: 9772505
after the test, I still get the same object type of "Research", but, it still gives me the same ClassCastException...
0
 

Author Comment

by:panpioneer
ID: 9772616

I know this sound unbelievable. But, I re-start the Tomcat server and clean up some of the files on our C drive to give C more space and it works fine now.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9772724
Well Tom does cache files and classes...
0
 
LVL 92

Expert Comment

by:objects
ID: 9774578
> ac = (Research)request.getAttribute("ab");    

Probably better to test that the stored attribute is actually a Research instance (as mentioned by tim earlier), rather than handle the exception when it occurs.

Object o = request.getAttribute("ab")
if (o instanceof Research)
{
   Research ac = (Research) o;
   // do what you need with Research instance
}
else
{
    // Not a Research instance
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
for i loop in grovy 1 46
Java JRE greater than 1.6 5 51
Why method in Java which is called from Runnable run() doesn't need to be 'static'? 1 22
servlet  URL Rewriting 1 28
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now