I can't figure out why my code is getting a StackOverFlow error. I'm trying to add a file as a node to its folder in a tree structure. I know the problem lies somewhere in the recursion, but I don't see why my function is resulting in infinite recursion. Here's my code:
private void addToFolders(String fileName, String folderName, Node<String> folders) {
/** Base case: if root node is the new folder, add file under it **/
if (folderName == folders.getData()) {
folders.addChild(fileName);
return;
}
/** If root node is not it, then look through its children **/
List<Node<String>> children = folders.getChildren();
for (int i = 0; i < children.size(); i++) {
addToFolders(fileName, folderName, children.get(i));
}
}
Please advice, and thanks for your help in advance.