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

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.
panpioneerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
> Is there a way to find out exactly what class the object is?

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimYatesCommented:
you can also do:

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

to see what it is...
0
TimYatesCommented:
Sorry...

System.out.println( o.getClass().getName() ) ;
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

panpioneerAuthor Commented:

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
CEHJCommented:
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
panpioneerAuthor Commented:
thanks. But, if I copy your code, I will get another error message: o is not defined....
0
CEHJCommented:
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
panpioneerAuthor Commented:
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
panpioneerAuthor Commented:

Oop! it is java.lang.NullPointerException
0
CEHJCommented:
Always test for null:

if (ac != null) {
   // you got it
}
0
panpioneerAuthor Commented:
after the test, I still get the same object type of "Research", but, it still gives me the same ClassCastException...
0
panpioneerAuthor Commented:

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
CEHJCommented:
Well Tom does cache files and classes...
0
Mick BarryJava DeveloperCommented:
> 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.