I need to add a child node (its name is stored in Object child1) to the specific parent node (Object hierarchy). To do that, I try to find the parent node and then add the child node to it. However, e.printStackTrace() provides the error message (see below). What am I doing wrong? Thanks!

private processHierarchyChild(Object hierarchy, Object child1) {

        MutableTreeNode childNode = new DefaultMutableTreeNode(child1.toString());

        try {
            TreePath path = tree1.getNextMatch(hierarchy.toString(), 0, Position.Bias.Forward);
        if (path == null) {
        } else {
            MutableTreeNode parentNode = (DefaultMutableTreeNode)(MutableTreeNode) path.getLastPathComponent();
            treeModel.insertNodeInto(childNode, parentNode, parentNode.getChildCount());
        } } catch (Exception e) {

        at SystClasses.TreeEditTest.processHierarchyChild(TreeEditTest.java:326)
        at SystClasses.TreeEditTest.processHierarchy(TreeEditTest.java:304)
        at SystClasses.TreeEditTest.<init>(TreeEditTest.java:78)
        at SystClasses.Form.createDocumentsLeftPanel(Form.java:252)
        at SystClasses.Form.createBase(Form.java:162)
        at SystClasses.Form.access$000(Form.java:44)
        at SystClasses.Form$2.actionPerformed(Form.java:143)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6267)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6032)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
6 Years
Discussion Span
Last Post by LianaN

The NPE happens when an object is null and its trying to be referenced (go figure). The lines do not line up with what you have posted but your error is occurring on line 326 of the TreeEditTest class. Take a look at that line and see what is causing the error. Something like this would cause a NPE:

Object o = null;

String s = o.toString(); //NPE Here


Line 326 in TreeEditTest is:

TreePath path = tree1.getNextMatch(hierarchy.toString(), 0, Position.Bias.Forward);

So, I suppose that the node with the name hierarchy.toString() cannot be found. But it is in the tree! What could be wrong here?


There could be a couple of problems with that line.

1) tree1 is null
2) hierarchy is null

Before that line put a couple of debugging print statements to see what the value is of both of those. So something like:

System.out.println("Tree1 Value: "+tree1);
System.out.println("Hierarchy Value: " + hierarchy);
TreePath path = tree1.getNextMatch(hierarchy.toString(), 0, Position.Bias.Forward);

yeah, you are absolutely right, i.e. tree1 = null. This is because I'm trying to find parents in the tree1 before creating the JTree:

DefaultMutableTreeNode root = processHierarchy(hierarchy);
        tree1 = new JTree(root);

But then the question is: does any procedure exist to set the root after creating the JTree?:

tree1 = new JTree();
DefaultMutableTreeNode root = processHierarchy(hierarchy);
// Something like: tree1.setRoot(root);

There are ways of setting the root of a tree after it has been created. If you are just using the default model (ie not your own) then you could get the tree's model then perform a cast, and then change the root

DefaultTreeModel model = (DefaultTreeModel)tree1.getModel();

Ok, thank you! The error message has disappeared and child nodes have been successfully added to the parent nodes.

Edited by LianaN: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.