在java中访问文件时获取错误

时间:2014-12-19 21:01:45

标签: java arrays file

我正在编写一个程序来检查重复文件并在表格中显示结果。我已经设置了一个表模型,并且还设置了一个按钮,该按钮将触发程序检查重复文件并将其显示在表中。但是我在桌面模型上显示时会出错。这是我的代码:

//This is the code that checks for duplicate files and displays it on the table model 
 public void findDuplicateFiles(File[] files) throws IOException {

    Map<String, List<File>> filesByHash = new HashMap<>();
    for (File file : files) {
        if (!file.isFile()) {
            continue;
        }

        String hash = MD5.asHex(MD5.getHash(file));

        List<File> filesForHash = filesByHash.get(hash);
        if (filesForHash == null) {
            filesByHash.put(hash, filesForHash = new ArrayList<>());
        }

        filesForHash.add(file);

    }

    for (Map.Entry<String, List<File>> entry : filesByHash.entrySet()) {
        List<File> filesForHash = entry.getValue();
        if (filesForHash.size() > 1) {
            String hash = entry.getKey();
            System.out.printf("%,d files have hash %s:%n",
                    filesForHash.size(), hash);
            int index = filesForHash.size() - 1;
            filesForHash.remove(index);

            for (File file : filesForHash) {

                System.out.println("  " + file);

                fileTableModel.setFiles(file.listFiles());
            }

        }
        //System.out.println(" No Duplicate Found ");

    }

}


 //This is the code that triggers a button and calls the above method for duplicate

                checkDup = new JButton("Find Duplicate");
        checkDup.setMnemonic('t');
        checkDup.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                try {
                    findDuplicateFiles(currentFile.listFiles());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                                }

        });
        toolBar.add(checkDup);



  //Here is the Table Model code.

   class FileTableModel extends AbstractTableModel {

public File[] files;
public FileSystemView fileSystemView = FileSystemView.getFileSystemView();
public String[] columns = {
    "Icon",
    "File",
    "Path/name",
    "Size",
    "Last Modified",

};

FileTableModel() {
    this(new File[0]);
}

FileTableModel(File[] files) {
    this.files = files;
}

public Object getValueAt(int row, int column) {
    File file = files[row];
    switch (column) {
        case 0:
            return fileSystemView.getSystemIcon(file);
        case 1:
            return fileSystemView.getSystemDisplayName(file);
        case 2:
            return file.getPath();
        case 3:
            return file.length();
        case 4:
            return file.lastModified();
        default:
            System.err.println("Fatal Error");
    }
    return "";
}

public int getColumnCount() {
    return columns.length;
}

public Class<?> getColumnClass(int column) {
    switch (column) {
        case 0:
            return ImageIcon.class;
        case 3:
            return Long.class;
        case 4:
            return Date.class;

    }
    return String.class;
}

public String getColumnName(int column) {
    return columns[column];
}

public int getRowCount() {
    return files.length;
}

public File getFile(int row) {
    return files[row];
}

public void setFiles(File[] file) {
    this.files = file;
    fireTableDataChanged();
}

}

这是我点击按钮时得到的错误代码:

        2 files have hash Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    da8f60e8474f7c89f368e5d6d379dcdc:
     C:\Users\Mbaegbu\Documents\Bandicam\bandicam 2014-07-02 10-55-03-421 - Copy.jpg
     at com.twmacinta.util.FileTableModel.getRowCount(DupFileBrowser.java:900)
     at javax.swing.JTable$ModelChange.<init>(Unknown Source)
     at javax.swing.JTable.sortedTableChanged(Unknown Source)
     at javax.swing.JTable.tableChanged(Unknown Source)
     at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
     at javax.swing.table.AbstractTableModel.fireTableDataChanged(Unknown Source)
     at com.twmacinta.util.FileTableModel.setFiles(DupFileBrowser.java:909)
     at com.twmacinta.util.DupFileBrowser.findDuplicateFiles(DupFileBrowser.java:418)
     at com.twmacinta.util.DupFileBrowser$10.actionPerformed(DupFileBrowser.java:325)
     at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
     at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
     at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
     at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
     at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)

我们将不胜感激。

1 个答案:

答案 0 :(得分:2)

您将调用File.listFiles()的结果传递给表模型的setFiles()方法。如果您调用它的文件不是目录,则listFiles()将返回null。这会导致您遇到的空指针异常。