Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.ClassCastException:

 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15286
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks a lot it worked
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic