4 Replies Latest reply: Dec 26, 2008 8:06 PM by DrClap RSS

    I get a strange NullPointerException when using a recursive method

    807589
      I found a code snippet that uses a recursive method for searhing files in folders and subfolders, but I get a NullPointerException when the path is set to "C:\\Users\\Roxxor\\Documents", but it works if the path is set to "C:\\Users\\Roxxor\\Documents\\Java".

      Why this extremly strange behaviour?

      import javax.swing.*;
      
      import javax.swing.tree.*;
      
      import java.io.*;
      
      
      
      public class DIC extends JFrame{
      
              public DIC() {
      
                      DefaultMutableTreeNode master = new DefaultMutableTreeNode("Node Title");
      
                      JTree tree = new JTree(master);
      
                      JScrollPane scroll = new JScrollPane(tree);
      
                      this.setSize(200,400);
      
                      this.getContentPane().add(scroll);
      
                      // pass the directory that you wish to list the contents
      
                      listAllFiles("C:\\Users\\Roxxor\\Documents\\Java",  master, true);         // Works
                     // listAllFiles("C:\\Users\\Roxxor\\Documents",  master, true);         // Throws an exception
      
                      setVisible(true);
                      pack();
      
              }
      
      
      
              /*
      
              * @params: String directory ~ the directory to scan
      
              * @params: DefaultMutableTreeNode parent ~ the node to add any files as children
      
              * @params: Boolean recursive ~ determine whether to scan all subdirectories, or just the parent
      
              */
      
              public static void listAllFiles(String directory, DefaultMutableTreeNode parent, Boolean recursive) {
      
                      File [] children = new File(directory).listFiles(); // list all the files in the directory
      
                      for (int i = 0; i < children.length; i++) { // loop through each
      
                              DefaultMutableTreeNode node = new DefaultMutableTreeNode(children.getName());

      // only display the node if it isn't a folder, and if this is a recursive call

      if (children[i].isDirectory() && recursive) {

      parent.add(node); // add as a child node
      listAllFiles(children[i].getPath(), node, true); // call again for the subdirectory

      } else if (!children[i].isDirectory()){ // otherwise, if it isn't a directory

      parent.add(node); // add it as a node and do nothing else

      }

      }


      }
      public static void main(String[] args)
      {
                     new DIC();
                }

      }
        • 1. Re: I get a strange NullPointerException when using a recursive method
          JoachimSauer
          Roxxor wrote:
          I found a code snippet that uses a recursive method for searhing files in folders and subfolders, but I get a NullPointerException when the path is set to "C:\\Users\\Roxxor\\Documents", but it works if the path is set to "C:\\Users\\Roxxor\\Documents\\Java".
          Always post the complete stack trace when you get an exception. Otherwise we'll be forced to guess and our crystal balls have been kind of unreliable lately.
          • 2. Re: I get a strange NullPointerException when using a recursive method
            807589
            The stack trace:
            java.lang.NullPointerException
                    at DIC.listAllFiles(DIC.java:50)
                    at DIC.listAllFiles(DIC.java:59)
                    at DIC.<init>(DIC.java:26)
                    at DIC.main(DIC.java:74)
            java.lang.NullPointerException
                    at DIC.listAllFiles(DIC.java:50)
                    at DIC.listAllFiles(DIC.java:59)
                    at DIC.<init>(DIC.java:26)
                    at DIC.main(DIC.java:74)
            java.lang.NullPointerException
                    at DIC.listAllFiles(DIC.java:50)
                    at DIC.listAllFiles(DIC.java:59)
                    at DIC.<init>(DIC.java:26)
                    at DIC.main(DIC.java:74)
            I changed the listAllFiles method to print the stack trace:
            public static void listAllFiles(String directory, DefaultMutableTreeNode parent, Boolean recursive) {
                           try{
            
                            File [] children = new File(directory).listFiles(); // list all the files in the directory
            
                            for (int i = 0; i < children.length; i++) { // loop through each
            
                                    DefaultMutableTreeNode node = new DefaultMutableTreeNode(children.getName());

            // only display the node if it isn't a folder, and if this is a recursive call

            if (children[i].isDirectory() && recursive) {

            parent.add(node); // add as a child node
            listAllFiles(children[i].getPath(), node, true); // call again for the subdirectory

            } else if (!children[i].isDirectory()){ // otherwise, if it isn't a directory

            parent.add(node); // add it as a node and do nothing else

            }

            }
                           }catch(NullPointerException err) { err.printStackTrace(); }


            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
            • 3. Re: I get a strange NullPointerException when using a recursive method
              807589
              listFiles returns null if you don't give it a directory.
              I suggest you use a debugger to trace the problem.
              • 4. Re: I get a strange NullPointerException when using a recursive method
                DrClap
                The signature of your method causes you to have to do a lot of converting between String and File objects. This is rather clumsy. I don't know if that is your problem, but simplifying the code never hurts.