-2

I don't know much about java and I have spend many days trying to find out what I am doing wrong. I have a JFrame form named FileManagerFrame and a Bean Form with a JTree inside. In JTree I have this code:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package FileManager;

import java.io.File;
import java.util.Collections;
import java.util.Vector;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * 
 */
public class FilesTree extends JTree 
{
private File dir;

/**
 * Creates new form FilesTree
 */
public FilesTree() 
{
    initComponents();
    //FilesTree tree = new FilesTree();
    File fList[] = File.listRoots();
      for(int i = 0; i  < fList.length; i++)
      {
          dir = fList[i];
          addNodes(null, dir);
      }


    // Add a listener
    addTreeSelectionListener(new TreeSelectionListener() 
    {
        public void valueChanged(TreeSelectionEvent e) 
        {
            DefaultMutableTreeNode node =(DefaultMutableTreeNode)e.getPath().getLastPathComponent();
            System.out.println("You selected " + node);
        }
    });
}

/** Add nodes from under "dir" into curTop. Highly recursive. */
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) 
{
   // String curPath = dir.getPath();
    DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(dir);
    if (curTop != null) 
    { // should only be null at root
        curTop.add(curDir);
    }
    Vector ol = new Vector();
    String[] tmp = dir.list();
    for (int i = 0; i < tmp.length; i++)
    {
        ol.addElement(tmp[i]);
    }
    Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
    File f;
    Vector files = new Vector();
    // Make two passes, one for Dirs and one for Files. This is #1.
    for (int i = 0; i < ol.size(); i++) 
    {
        String thisObject = (String) ol.elementAt(i);
        String newPath;
        if  (".".equals(dir.getName()))
        {
            newPath = thisObject;
        }
        else
        {
            newPath = dir + File.separator + thisObject;
        }
        if ((f = new File(newPath)).isDirectory())
        {
            addNodes(curDir, f);
        }
        else
        {
            files.addElement(thisObject);
        }
    }
    // Pass two: for files.
    for (int fnum = 0; fnum < files.size(); fnum++)
    {
        curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
    }
    return curDir;
}

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

It suppose to display in JTree all system root files. Can anyone tell me why it is not working and suggest what I can do?

Here is the stack trace : http://prntscr.com/3n6st2

Kiki
  • 75
  • 1
  • 1
  • 7
  • 1
    *What* exactly does not work? – Steffen May 27 '14 at 12:58
  • 1
    Is it giving any exceptions? – Sanjeev May 27 '14 at 13:03
  • @Sanjeev When i run the program it gives me this: http://prntscr.com/3n3csm – Kiki May 27 '14 at 16:05
  • *"hen i run the program it gives me this: prntscr.com/3n3csm"* "ERROR - The requested URL could not be retrieved" One of the many reasons you should [edit the stack trace into the question](http://stackoverflow.com/posts/23890002/edit). – Andrew Thompson May 28 '14 at 00:18
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). BTW - why does this code extend `JTree`? I've used trees hundreds of times in my 10+ years of GUI programming in Java, the number of times I've needed to extend it is ..0. Given this seems to be dealing with files, you might start with the code for [File Browser GUI](http://codereview.stackexchange.com/q/4446/7784). – Andrew Thompson May 28 '14 at 00:20

1 Answers1

1

I think you should read more about Java and OOP. Your code fragment will provoke stackoverflow due to endless recursion.

public FilesTree() 
{
    initComponents();
    FilesTree tree = new FilesTree();

First of all you should remove the last line and all occurencies of variable tree. When your code still not work please provide more information (for example stack trace) if you want to get answer.

Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • I did what you said and I posted tha stack trace. – Kiki May 27 '14 at 16:44
  • Update your code plz. At the line 62 you cannot get NPE. And the statement dir.equals(".") will always return false. Better is: ".".equals(dir.getName()) – Sergiy Medvynskyy May 27 '14 at 18:49
  • I did. I am so so sorry I erase some comments and thats why the lines and the stack trace wasnt correct. I'm new to java sorry for the stupid mistakes. I really need to get the code to work so your help is valuable. Thanks a lot for your time. – Kiki May 27 '14 at 21:05
  • dir.list() may return null if it's not a directory. You should to prevent using of null values. – Sergiy Medvynskyy May 27 '14 at 21:29