Results 1 to 2 of 2

Thread: insertNodeInto

  1. #1
    dauphin is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default insertNodeInto

    Hi,

    I'm really frustrated about an error at DefaultTreeModel.insertNodeInto. Here's the part of the code:
    Java Code:
    ...
    	int i;
    	int j;
    
    	int grph = aspect.get(0).graph.size(); 
    	// aspect and graphs are too ArrayLists, there is apparently no problem with them in this code. 
    	// They are tested just before this part. 
    	int rootcc;
    	int chldcc;
    
    	DefaultTreeModel m = (DefaultTreeModel) trGraph.getModel(); // trGraph is a JTree component
    	DefaultMutableTreeNode root = (DefaultMutableTreeNode) m.getRoot();
    	DefaultMutableTreeNode n;
    
    	root.removeAllChildren();
    
    	// ---------
    	for (j = 0; j < grph; j++) {
    		n = new DefaultMutableTreeNode(aspect.get(0).graph.get(j).getName());
    	 	root.add(n);
    		aspct = aspect.size();
    		for (i = 0; i < aspct; i++) {
    			n = new DefaultMutableTreeNode(aspect.get(i).getName());
    			rootcc = root.getChildCount();
    			chldcc = root.getChildAt(j).getChildCount();
    			m.insertNodeInto(n, (DefaultMutableTreeNode) root.getChildAt(j), i); // here the error is raised, at i=1
    		}
    	}
    	....
    The value of selected variables during debug before the error line:
    Java Code:
    j		0	// first "for" loop
    i		1	// second "for" loop
    grph		1	 
    aspct		2
    rootcc	1
    chldcc	1
    n		!null
    Apparently the error is related to the number of the children of root.childAt(j):
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
    at java.util.Vector.elementAt(Unknown Source)
    at javax.swing.tree.DefaultMutableTreeNode.getChildAt (Unknown Source)
    at javax.swing.tree.DefaultTreeModel.getChild(Unknown Source)
    at javax.swing.tree.VariableHeightLayoutCache.createN odeAt(Unknown Source)
    at javax.swing.tree.VariableHeightLayoutCache.treeNod esInserted(Unknown Source)
    at javax.swing.plaf.basic.BasicTreeUI$Handler.treeNod esInserted(Unknown Source)
    at javax.swing.tree.DefaultTreeModel.fireTreeNodesIns erted(Unknown Source)
    at javax.swing.tree.DefaultTreeModel.nodesWereInserte d(Unknown Source)
    at javax.swing.tree.DefaultTreeModel.insertNodeInto(U nknown Source)
    However, obviously chldcc value is 1 at i=1, which means root.childAt(j) has 1 child. And as i is also 1, there shouldn't be a problem to add a node to location 1 of that root's child.

    What've I possibly got wrong?

    Thanks in advance.
    Last edited by dauphin; 01-18-2014 at 02:19 PM.

  2. #2
    dauphin is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default Re: insertNodeInto

    Solved,

    I'd forgotten to put the model reload() after removing all children.

Similar Threads

  1. Replies: 2
    Last Post: 11-30-2010, 08:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •