“AWT-EventQueue-0” java.lang.StackOverflowError - While trying to create JTree folder structure
“AWT-EventQueue-0” java.lang.StackOverflowError - While trying to create JTree folder structure
I'm trying to build a JTree folder structure from a output. Here is the approach.
JTree
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
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. 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 Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
– Andrew Thompson
Aug 22 at 6: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 at 15:51
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
less compared to what? where is the error thrown? what is the complete stacktrace?
– Stultuske
Aug 22 at 6:36