jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes tree.getLastSelectedPathComponent returns null ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "tree.getLastSelectedPathComponent returns null !" Watch "tree.getLastSelectedPathComponent returns null !" New topic
Author

tree.getLastSelectedPathComponent returns null !

sherif amer
Greenhorn

Joined: Oct 24, 2011
Posts: 2
Hi,
I am creating a tree and when the below code run it gives null pointer exception as the getLastSelectedPathComponent returns null

logger.debug("tree.getLastSelectedPathComponent() = "+tree.getLastSelectedPathComponent());
String selectedNode=tree.getLastSelectedPathComponent().toString();

any Idea why this happening it was working just yesterday !!!
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

if nothing is selected in the tree, the call will return null. If you need further help, post your SSCCE


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
sherif amer
Greenhorn

Joined: Oct 24, 2011
Posts: 2
Actually i am getting the tree using the below code
tree = ivrTree.getIVRTree(productType); // getIVRTree is the function which construct the tree based on DB selection.
tree.addTreeSelectionListener(this);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

and then i am handling the listener
public void valueChanged(TreeSelectionEvent e) {
logger.debug("inside valueChanged");
String selectedNode=tree.getLastSelectedPathComponent().toString(); //this line always throw the null pointer exception as the tree.getLastSelectedPathComponent() coming with null.
String node=selectedNode.substring(0,selectedNode.indexOf("."));
logger.debug("node = "+node);
logger.debug("controller= "+controller);
controller.setSubMenu(node);
System.out.println(node);
}

Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
And welcome to the Ranch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You'll not only get a valueChanged event when something is selected, but also when something is unselected. And when you get this event for the unselection, the tree's selected path is null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tree.getLastSelectedPathComponent returns null !
 
Similar Threads
JTree Null Pointer After Refresh
want to remove node from dom
How to retrieve an object stored in a tree.DefaultMutableTreeNode?
how do I get the current node selection in a Jtree?
getLastSelectedPathComponent returns null