首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java中访问文件时获取错误

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

Stack Overflow用户
提问于 2014-12-19 21:01:45
回答 1查看 87关注 0票数 0

我正在编写一个程序,用于检查重复文件并在表中显示结果。我已经建立了一个表格模型,也设置了一个按钮,它将触发程序检查重复文件并在表上显示它们。但我在表模型上显示错误时会发现错误。这是我的代码:

代码语言:javascript
复制
//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();
}

}

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

代码语言:javascript
复制
        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)

你的帮助将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-19 21:27:22

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27573841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档