• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.ClassCastException:

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
i have the following program to do an xpath query , when i run it i get the following error ..anyidea why??


Error
java.lang.ClassCastException: com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList


thanks a lot
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently, you are attempting to cast an object to a type that it does not belong to. I'm not sure which line is causing the exception, since you didn't provide the stack trace or line numbers, but I'm assuming it's the cast to (ListNodeList).

Look, xpath.evaluate will not return an instance of ListNodeList, because you have created that class, so xpath doesn't even know that it exists. It's actually returning a DTMNodeList, which is not a subtype of ListNodeList. So you cannot cast DTMNodeList as a ListNodeList. If you want to convert from one to the other, you will have to create a method that will do the conversion.

To do this, you will need to figure out what the "important" members of DTMNodeList are (the ones that you care about), and extract them from the instance of DTMNodeList and use them to create your ListNodeList. Then you can use the methods you created in your ListNodeList class. (Basically, you wil need to obtain the list of nodes from the DTMNodeList and use it in the constructor of your ListNodeList).

- Adam
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but DTMNodeList is not a part of java ?

and this error comes when running not when compiling so i do not know where it is ??
[ July 18, 2006: Message edited by: Maha Hassan ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are casting the result of xpath.evaluate(...) to your own class ListNodeList. You get the error because the method returns an object of type com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList, which is not a ListNodeList.

Indeed, DTMNodeList is not a part of the standard Java API, but that's not the problem.

The method xpath.evaluate(...) doesn't know your class ListNodeList, so how can it ever return an object of type ListNodeList?

You just have to use org.w3c.dom.NodeList and not your own class ListNodeList:
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot it worked
 
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic