emekamba
emekamba

Reputation: 188

Get Errors when accessing file in java

I am writing a program that checks for duplicate files and displays the result in a table. I have set up a table model and have also set up a button that will trigger the program to check for duplicate files and displays them on the table. But i get errors while displaying them on the table model. Here is my code:

//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();
}

}

This is the error code I get when ever I click on the button:

        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)

Your help will be appreciated.

Upvotes: 0

Views: 91

Answers (1)

yole
yole

Reputation: 97113

You're passing the result of calling File.listFiles() to the setFiles() method of your table model. listFiles() will return null if the file on which you're calling it is not a directory. This leads to the null pointer exception that you've run into.

Upvotes: 2

Related Questions