2016-11-27 174 views
0

以下代碼是FileTreeModel的修改版本,僅顯示目錄。然而。我在嘗試訪問需要管理員權限的文件夾時收到空指針異常。一個例子可能看起來像目錄資源管理器獲取NPE

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at DirectoryExplorer$FileTreeModel.listDirectories(DirectoryExplorer.java:112) 
    at DirectoryExplorer$FileTreeModel.getChildCount(DirectoryExplorer.java:79) 
    at javax.swing.tree.VariableHeightLayoutCache$TreeStateNode.expand(Unknown Source) 
    at javax.swing.tree.VariableHeightLayoutCache$TreeStateNode.expand(Unknown Source) 
    at javax.swing.tree.VariableHeightLayoutCache.ensurePathIsExpanded(Unknown Source) 
    at javax.swing.tree.VariableHeightLayoutCache.setExpandedState(Unknown Source) 
    at javax.swing.plaf.basic.BasicTreeUI.updateExpandedDescendants(Unknown Source) 
    at javax.swing.plaf.basic.BasicTreeUI$Handler.treeExpanded(Unknown Source) 
    at javax.swing.JTree.fireTreeExpanded(Unknown Source) 
    at javax.swing.JTree.setExpandedState(Unknown Source) 
    at javax.swing.JTree.expandPath(Unknown Source) 
    at javax.swing.plaf.basic.BasicTreeUI.toggleExpandState(Unknown Source) 
    at javax.swing.plaf.basic.BasicTreeUI.handleExpandControlClick(Unknown Source) 
    at javax.swing.plaf.basic.BasicTreeUI.checkForClickInExpandControl(Unknown Source) 
    at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(Unknown Source) 
    at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$500(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

要設法限制這個我添加了一個論點,即只增加了可見的文件夾到JTree,但是,這並不趕上他們。有沒有辦法排除需要探索管理員權限的所有文件夾?

MCVE/SSCCE

import java.awt.GridLayout; 

import java.io.File; 

import java.util.Arrays; 
import java.util.ArrayList; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTree; 
import javax.swing.tree.TreeModel; 


@SuppressWarnings("serial") 
public class DirectoryExplorer extends JFrame { 
    private DirectoryExplorer() { 
     super("Directory Explorer"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new GridLayout(1, 1)); 
     createPanel(); 
     pack(); 
     setVisible(true); 
    } 


    private void createPanel() { 
     JPanel panel = new JPanel(new GridLayout(1, 1)); 

     TreeModel model = new FileTreeModel(new File("C:/")); 
     JTree tree = new JTree(model); 
     tree.setShowsRootHandles(true); 
     tree.collapseRow(0); 

     panel.add(new JScrollPane(tree)); 
     getContentPane().add(panel); 
    } 

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

    private static class FileTreeModel implements TreeModel { 

     private File root; 

     public FileTreeModel(File root) { 
      this.root = root; 
     } 

     @Override 
     public void addTreeModelListener(javax.swing.event.TreeModelListener l) { 
      //do nothing 
     } 

     @Override 
     public Object getChild(Object parent, int index) { 
      File f = (File) parent; 
      return listDirectories(f)[index]; 
     } 

     @Override 
     public int getChildCount(Object parent) { 
      File f = (File) parent; 
      if (!f.isDirectory()) { 
       return 0; 
      } else { 
       return listDirectories(f).length; 
      } 
     } 

     @Override 
     public int getIndexOfChild(Object parent, Object child) { 
      File par = (File) parent; 
      File ch = (File) child; 
      return Arrays.asList(listDirectories(par)).indexOf(ch); 
     } 

     @Override 
     public Object getRoot() { 
      return root; 
     } 

     @Override 
     public boolean isLeaf(Object node) { 
      return false; 
     } 

     @Override 
     public void removeTreeModelListener(javax.swing.event.TreeModelListener l) { 
      //do nothing 
     } 

     @Override 
     public void valueForPathChanged(javax.swing.tree.TreePath path, Object newValue) { 
      //do nothing 
     } 

     private File[] listDirectories(File path) { 
      ArrayList<File> arrayList = new ArrayList<File>(); 
      for(File temp : path.listFiles()) { 
       if(temp.isDirectory() && !temp.isHidden()) 
        arrayList.add(temp); 
      } 

      return arrayList.toArray(new File[0]); 
     } 
    } 
} 
+0

另請參閱[文件瀏覽器GUI](http://codereview.stackexchange.com/q/4446/7784)。 –

+0

@AndrewThompson儘可能多的我喜歡你的文件資源管理器有沒有辦法檢測到需要管理員權限的文件夾(或者當電腦在windows下運行時無法訪問) – Dan

+0

*「是否有辦法檢測文件夾需要管理員權限「*不,我從來沒有編碼它這樣做。你的? –

回答

1

File.listFiles()可以返回null如果給定文件是不是一個目錄或無法讀取。見JavaDoc

if(temp.isDirectory() && !temp.isHidden() && temp.listFiles()!=null) 
+0

爲什麼不呢?跳過這些文件夾。 – brummfondel

+0

如何檢查跳過之前是否可以訪問它們? – Dan

+0

我修改了評論。 – brummfondel