Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException while casting to XMLTreeNode(JTree from XML)

 
Vaijan Hundlekar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the statement on which I am getting ClasscastException:

public void visitAllNodes() {
//System.out.println(tree.getModel().getRoot());
XMLTreeNode root = (XMLTreeNode)tree.getModel().getRoot(); //getting exception
visitAllNodes(root);
}
I am Using XMLTreeModel, XMLTreeNode,XMLTreePanel classes for making a JTree from a XML.
And above method for visiting all nodes, but getting an exception.
I am not able to figure out why this is happening, please help.
 
Paul Clapham
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's happening because the getRoot() method returns something which isn't an XMLTreeNode or a subclass of it. That's by definition what ClassCastException always tells you.
 
Vaijan Hundlekar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.

This is the getRoot() function:
public Object getRoot() {
if (document==null) {
return null;
}
Vector<Element> elements = getChildElements(document);
if (elements.size() > 0) {
return new XMLTreeNode( elements.get(0));
}
else {
return null;
}
}
And i checked it, its not returning null, so wat is the exact problem why it isnt able to cast it.
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure this model is being used?
Try the following:

This will tell you exactly of what class both the TreeModel and its root object are.
 
Vaijan Hundlekar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Rob,
thanks..i tried what you asked for and got the following output:
class com.TestDb.XMLTreeModel
class com.TestDb.XMLTreeNode
Now i have to cast it in XMLTreeNode...


 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so the class should be right. Do you happen to have another XMLTreeNode class somewhere, and have imported that one? If so, you think you're casting to com.TestDb.XMLTreeNode but in fact you may be trying to cast to xxx.yyy.XMLTreeNode.
 
Vaijan Hundlekar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob that worked!
Now i am having one problem:

Below function is in XMLTreePanel:

This is my XMLTreeNode Class:
package com.TestDb;


can you be able to help me in writing a getPath() function in XMLTreeNode class?


Actually i want to implement a search function which after getting the required string in a tree node it shud scroll to display the node in a JTree.

 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, please Use Code Tags. It will preserve your indentation.

I would implement getPath() as follows:
 
Vaijan Hundlekar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
i tried your function but i think node.getParent() is not implemented.
See my aim is to search a node in a JTree and display it to user(select/highlight), and i tried to do it but no success could you please write a search method which will select/highlight/color a node in XMLTreePanel class given below.
i am pasting all the classes here :
XMLTreeNode: pasted in my previous post

XMLTreePanel:



XMLTreeModel:

 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaijan Hundlekar wrote:i tried your function but i think node.getParent() is not implemented.

Then XMLTreeNode does not implement TreeNode. You will need this method, so if it isn't there add it.

could you please write a search method which will select/highlight/color a node in XMLTreePanel class given below.

No. Just because I have given you the code for a pretty simple method doesn't mean I will do all the work for you.
You should check out the JTree API; JTree.setSelectionPath should do the trick. But for that you need a TreePath.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic