0

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.

4
  • According to the stack trace, the problem is actually in class FileTableModel. Could you supply that class as well? Commented Dec 19, 2014 at 21:05
  • Ok I will update it right away... Commented Dec 19, 2014 at 21:13
  • You use hashes, but even though the probability of a collision is probably low, it is not because two files' contents have the same hash value that their contents are the same Commented Dec 19, 2014 at 21:26
  • 1
    Also, this is 2014 and soon to be 2015, so you really should be using java.nio.file Commented Dec 19, 2014 at 21:27

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@yole what is your suggestion, what should i do in this case.
As far as I can see from your code, the file on which you're calling listFiles() is never a directory, so I have no idea how you expected this code to work. What do you actually want to show in your table?
If you want to do a recursive tree walk, use Files.walkFileTree(); after that, it depends on what you want to do
@yole I want to show the list of duplicate regular files path on the table.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.