Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8829895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:56:25+00:00 2026-06-14T07:56:25+00:00

I’m learning to JTrees and Java. Constructive suggestions and feedback are very welcome. I

  • 0

I’m learning to JTrees and Java.
Constructive suggestions and feedback are very welcome.

I think I am missing some understanding of JTrees and after 5 hours Googling, and testing I’m stuck. I’ve simplified the code as much as possible.

        public void actionPerformed(ActionEvent event) {
            MyNode selNode = (MyNode) m_tree.getLastSelectedPathComponent();
            if (selNode != null) {
                MyNode newNode = new MyNode("New Node");
                model.insertNodeInto(newNode, selNode,
                        selNode.getChildCount());
                MyNode[] nodes = model.getPathToRoot(newNode);
                TreePath path = new TreePath(nodes);
                m_tree.scrollPathToVisible(path);
                m_tree.setSelectionPath(path);
                // *******   The next line throws the exception shown below.  ****
                m_tree.startEditingAtPath(path);
            }


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicTreeUI.startEditing(BasicTreeUI.java:2059)
at javax.swing.plaf.basic.BasicTreeUI.startEditingAtPath(BasicTreeUI.java:601)
at javax.swing.JTree.startEditingAtPath(JTree.java:2349)
at ItemCreator.ItemCreator$1.actionPerformed(ItemCreator.java:74)

Code – My Simple Mutable JTree

1) When adding a new node into the JTree the code throws Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException

2) Any general constructive feedback very welcome.

Kind regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:56:26+00:00Added an answer on June 14, 2026 at 7:56 am

    The problem is not the startEditingPath, but the incorrect model implementation. Basically its fails to notify its listeners on changes, consequently the ui has no chance to update its internals to include the added node.

    The model fails in

    1. not accepting listeners (empty implementation of addTreeModelListener)
    2. not even trying to fire after changing (on inserts, updates …)
    3. incorrect getPathToRoot implementation

    It’s not entirely trivial and the java doc slightly (to put it mildly) confusing – that’s why SwingX has a well-tested utility class TreeModelSupport which takes over the burden. It can be used standalone, or as a blue-print of how-to do it.

    In your custom model, some relevant changes (incomplete, the other modification methods must be fixed accordingly, as must the remove listener):

    // prepare fix issue 1: instantiate the notification support    
    private TreeModelSupport support;
    
    public ItemTreeModel(MyNode root) {
        this.root = root;
        support = new TreeModelSupport(this);
        // remove the following: a model never listens to itself
        // this.addTreeModelListener(new MyTreeModelListener());
    }
    
    // fix issue 1: accept listener
    public void addTreeModelListener(TreeModelListener l) {
        support.addTreeModelListener(l);
    }
    
    // fix issue 2: notify the listeners on inserts
    public void insertNodeInto(final MyNode newNode, final MyNode selNode,
            final int childCount) {
        selNode.add(childCount, newNode);
        newNode.setLocation(selNode);
        support.fireChildAdded(new TreePath(getPathToRoot(selNode)),
                childCount, newNode);
    }
    
    // fix issue 3: pathToRoot as needed in TreePath 
    public MyNode[] getPathToRoot(MyNode node) {
        ArrayList<MyNode> itemArrayList = new ArrayList<MyNode>();
        // TODO - Add root node ?
        // yes, certainly - please read the java doc for TreePath
        while ((node != null)) { // && (node != root)) {
            itemArrayList.add(0, node);
            node = node.getLocation();
        }
        return itemArrayList
                .toArray(new MyNode[itemArrayList.size()]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.