Diego Penha
Diego Penha

Reputation: 392

File loading using getClass().getResource("...").getFile()

first topic on stackoverflow, been using it for years, but can't find a solution to this: I need to fill a JTable with names of files in a directory (using Eclipse IDE).

im using this:

 public void getFilesName()
    {
        File dir = new File(getClass().getResource("C:\\Files\\Server\\").getFile());

        File[] files = dir.listFiles();

        modelo.setColumnIdentifiers(new String[]{"Files Names"});

        Object[] row = new Object[1];

        for(int i = 0; i < files.length; i++)
        {
            row[0] = files[i].getName();

            modelo.addRow(row);
        }
    }

but it returns:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

and I can't find a way to do it.

Upvotes: 0

Views: 1121

Answers (1)

Sagar Chaudhari
Sagar Chaudhari

Reputation: 155

Try to use:

File dir = new File("C:\\Files\\Server\\");
File[] files = dir.listFiles();

Instead of:

File dir = new File(getClass().getResource("C:\\Files\\Server\\").getFile());
File[] files = dir.listFiles();

Upvotes: 1

Related Questions