0

I'm trying to build a JTree folder structure from a output. Here is the approach.

  • I call a system
  • get list of folders
  • loop through list of folders and create a list
  • create a folder structure from the list

This is working fine when the number of folders are less however when the number of folders are more it is throwing a stack overflow error.

private static DefaultMutableTreeNode builtTreeNode(String comptype) {
        try{
            List<Node> nodeList = new ArrayList<>();
            //Defin the configuration context
            APIObject[] fol =  //Call the other system to get folders;
            DefaultMutableTreeNode dmtNode = null;
            System.out.println(" Folder Size " +fol.length);
             for (int i = 0; i < fol.length; i++) { 
                    if(getParentFolder().getName() == null && !getParentFolder().getIsActive()){
                    dmtNode = new DefaultMutableTreeNode(df.getName().toString());
                    nodeList.add(new Node(df.getName().toString()));// Set the ROOT folder in nodelist
                }           
                if(getParentFolder().getName() != null ){
                                //nodeList.add(new Node("Child", "Parent")); 
                                //add remaining folders to nodelist
                                nodeList.add(new Node(df.getName(), df.getParentFolder().getName())); 
                            }               
             }       
             findChild(dmtNode, nodeList); //
             return dmtNode;            
            }
private static void findChild(DefaultMutableTreeNode parent, List<Node> list) {
        for (int i = 0; i < list.size(); i++) {
          if (list.get(i).parent != null && list.get(i).parent.equals(parent.toString())) {
            DefaultMutableTreeNode child = new DefaultMutableTreeNode();
            child = new DefaultMutableTreeNode(list.get(i).label);
            parent.add(child); // Getting exception from this line of code
            findChild(child, list); // child of child
          }
        }
      }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sanjay
  • 3
  • 3
  • less compared to what? where is the error thrown? what is the complete stacktrace? – Stultuske Aug 22 '18 at 06:36
  • It's probably best not to try and create the entire tree at once. Even before a stack overflow, it will contain far more files that the user could ever trawl through. Instead look to create it lazily, as done in the [File Browser GUI](http://codereview.stackexchange.com/q/4446/7784). That code will fill only the top level directories, waiting for user selection of a directory before populating the files within it. General tip: For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Aug 22 '18 at 06:48
  • @Stultuske its thrown in from these lines - parent.add(child); // Getting exception from this line of code findChild(child, list); // child of child Below is the error - Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at javax.swing.tree.DefaultMutableTreeNode.isNodeAncestor(Unknown Source) at javax.swing.tree.DefaultMutableTreeNode.insert(Unknown Source) at javax.swing.tree.DefaultMutableTreeNode.add(Unknown Source) at com.exacttarget.login.Login.findChild(Login.java:634) – Sanjay Aug 22 '18 at 15:51

0 Answers0