aspose file tools
The moose likes Java in General and the fly likes Class Cast exception Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Class Cast exception" Watch "Class Cast exception" New topic
Author

Class Cast exception

Saurabh udgirs
Greenhorn

Joined: May 20, 2009
Posts: 4
Hi,
I am getting a class cast exception.
the getVisibleInSitemap() method returns string.
Here is the code

Can anyone let me know how should I overcome this problem
regards,
Saurabh
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
Welcome to JavaRanch.

The cast is "(LinkVO)". So it seems that not all elements of linksArrayList are, in fact, LinkVO objects. You can check that by "System.out.println(linksArrayList.get(i).getClass().getName())"


Android appsImageJ pluginsJava web charts
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
If Java had generics from the outset, people would always use generics and this sort of problem would be very unusual. Look at the tutorial link I just quoted.
Saurabh udgirs
Greenhorn

Joined: May 20, 2009
Posts: 4
Thanks for your inputs
I had put this,
and this is what I got,


By what, we can get rid of this exception...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
That looks like nodes from a DOM tree. Where/how are you putting LinkVO objects in that list?
Saurabh udgirs
Greenhorn

Joined: May 20, 2009
Posts: 4
Hi,
Its been put in a different class, this is how it happens


and this is where linksArrayList is set

The setsublink array in above snippet is just a setter method which sets the value.
Let me know in case you require some other details.
regards,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
I can't say I understand what's going on (for instance, what does "LinksList" do, and what does it return), but *somehow* objects other than LinkVO are getting into the list. There can't be that many spots in the code where elements of the list are set or added, right?
Saurabh udgirs
Greenhorn

Joined: May 20, 2009
Posts: 4
Hi,
Sorry to have missed that,
LinksList is the same method where the code is extracted from, so it is a recursive method
It returns linksArrayList,
And dont think there is any other way by which this arraylist is getting populated.
Also, surprisingly, I am being told that is code is working perfectly fine on one of my colleagues system, Is it related to some java version then or all the java classes in the workspace needs to be rebuilt...? Perhaps I feel that this issue is a code issue and not environmental,
Whats your call?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
I think this is no longer a "beginner's" thread. Moving.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3268

I would use System.out.println(linksArrayList.get(i).getClass().getName()); _before_ you insert items into the list, see where those DOM nodes are coming from.


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Cast exception
 
Similar Threads
cast and insatnce operator
Following code shows ClassCastException
Upcasting from Class to interface Problem
Reason for ClassCastException
When does Class Cast Exception occur?