Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HashMap -- Whats happening ??? (Urgent)

Status
Not open for further replies.

tlohan

Programmer
Sep 14, 2001
19
BE
Hi,

I have implemented a JTree.
The tree can be expanded by clicking the handles beside the nodes and when the nodes are clicked on, any information pertaining to that node is displayed. I have also implemented a search function for the tree, which allows the user to click a button and a dialog appears. The user can enter the name of the node they are looking for and the tree expands to show the node and the information is displayed.

However I have found that if I use the search function on a node in the tree and then use click method to find the same node, and finally try and use the search method again the tree fails to expand on the last attempt.

I have traced this problem down to a HashMap but am unable to figure out why the problem is occurring. When the table is being built I have a method that is used to build the tree:

DefaultMutableTreeNode child = node;
child = new DefaultMutableTreeNode(Node);
HashMap.put(Node, child);
node.add(child);

Each node is added to the tree successfully. When I initiate my search function the following is called:

DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) HashMap.get(Node);

Debugging this reveals that it works perfectly for each Node item until I use the click method. When I click on a node another method is called which gets the location of the click and returns the TreePath to the node. This method works perfectly and always displays the information. Naturally the tree is expanded as the user has to expand the tree to selected the node.

Now when I use the search method again, and look for the same Node, or a Node within the table, I find that the code

DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) HashMap.get(Node);

returns a null.

Why is this ??
Why is the key, the Node, present while I use the search method but once the click method is used, does this key no longer exist in the HashMap.

Thanks for your help
 
What class is
Code:
Node
? Is it a custom class or standard in the API? If it is a class that you wrote, did you make sure to override the
Code:
hashcode()
method? If not then it is not going to work correctly in
Code:
Map
objects. Lastly, you should try to avoid capitalizing variables, it makes it too easy to confuse them with classes.
 
Cheers.

Node is actually something I put in for mibNode which is a variable which represents a third-party custom class. hashcode() has not been overwritten but the application actually works fine if I keep implementing the search method. its when I combine the search method with the click method that trouble results. I am currently looking at implementing a TreeSelectionListener instead of the click method. However I am having trouble with the search method. TreeSelectionListener takes a TreeSelectionEvent. How do I create an one of these in the search method if I am only taking in the name of the node ??

Thanks,

Tom
 
I'm not sure about TreeSelectionListener, but do you at any time alter the dmtn in your click-and-expand method? Or do you alter Node at all?

If you get a set of the values in the HashMap, (by say doing HashMap.values()), and remove one of those values from the Set or the Vector that results, you will remove the key-value pair from the HashMap as well.

If you alter Node at all, it may produce a different hashcode. So if you do something like:
HashMap.put(Node, child);
Node.change(); //alter Node in some way
HashMap.get(Node);
it might look for a different Node than the one you originally put in.

Also, are you sure that the Node is still a key? If you look up the value for a key that is not in the HashMap, it will also return null. For debugging purposes, you might want to do HashMap.containsKey(Node) just in case you may have somehow removed the key-value pair in the click-and-expand method.
 
Hi,

Messing around with the code I found that when adding the
objects to the HashMap I had:

HashMap.put(Node, child);

Changing this to:

HashMap.put(Node.toString(), child);

appears to have solved the problem. As you have said it appears as if the node has been altered in the meantime. I know that it is reloaded into the system when I used the click and valueChanged methods, however, I had not expected that this would cause a problem.

Thanks for all the help.

Much appreciated.
 
I think that since the Node class didn't override hashCode() then it recieved the default implementation in Object. This would cause your odd functionality. Anything that will go into a Map needs to override hashCode() and provide a suitable implementation, otherwise you are asking for wierd results because even if node1.equals(node2) returns true the hashCode() for these two objects could be different.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top