-1

I am trying to find the last modified version of a file, I have this working but when i try to find a certain file I get a NULLPointerException. My code :

public static File getFile(String dir, String chat) {

    File fl = new File(dir);
    File[] files = fl.listFiles(new FileFilter() {          
        public boolean accept(File file) {
            return file.isFile();
        }
    });
    long lastMod = Long.MIN_VALUE;
    File choice = null;
    for (File file : files) {
        if (file.lastModified() > lastMod && file.getName().contains(chat)) {
            choice = file;
            lastMod = file.lastModified();
        }
    }
    return choice;
}

The code works when you take the "&& file.getName().contains(chat)" out. Otherwise, it has been given me NullPointerException error.

I know that something like this works because I had it working but needed to start from scratch with my code :(

Hakan Serce
  • 11,198
  • 3
  • 29
  • 48

2 Answers2

0

Here is where the NullPointerException comes from.

File choice = null;

You need to instantiate it before calling it here --> choice = file;

File choice = new File(dir);
display name
  • 4,165
  • 2
  • 27
  • 52
  • There is another possible NPE: `.listFiles()` – fge Jan 14 '15 at 23:13
  • I disagree because the user said that it worked unless he made changes in the following loop so the error must be inside the loop. – display name Jan 14 '15 at 23:14
  • This is, however, a possible NPE nevertheless. Try and `.listFiles()` on a directory from which you cannot read entries for instance; or from a `File` which isn't a directory... – fge Jan 14 '15 at 23:15
  • There are tons of other ways to improve this base code, but it is important to address the issue in this code before trying to throw out a generic method. I understand you want your answer to stand out, but I aim for clarity and right to the point where the user needs. Thanks. – display name Jan 14 '15 at 23:17
  • This is not that I want my answer to stand out. But using `File` in 2015 is an [error](http://java7fs.wikia.com/wiki/Why_File_sucks). Unfortunately, Android users don't have a choice but the OP is using Java 7+. – fge Jan 14 '15 at 23:20
0

Here is a version using java.nio.file:

public static Path getMostRecent(final String dir, final String chat)
    throws IOException
{
    final Path dirpath = Paths.get(dir).toAbsolutePath();
    Path ret = null;
    FileTime mostRecent = FileTime.fromMillis(0L); // Jan 1st 1970, 00:00:00 GMT
    FileTime current;

    final DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<>()
    {
        @Override
        public boolean accept(final Path entry)
        {
            return Files.isRegularFile(entry)
                && entry.getFileName().toString().contains(chat);
        }
    };

    for (final Path entry: Files.newDirectoryStream(dirpath, filter)) {
        current = Files.getLastModifiedTime(entry);
        if (current.compareTo(mostRecent) > 0) {
            mostRecent = current;
            ret = entry;
        }
    }

    return entry;
}

If you use Java 8:

public static Path getMostRecent(final String dir, final String chat)
    throws IOException
{
    final Path dirpath = Paths.get(dir).toAbsolutePath();
    try (
        final Stream<Path> entries = Files.list(dirpath);
    ) {
        return entries.filter(Files::isRegularFile)
            .filter(entry -> entry.getFileName().toString().contains(chat))
            .sorted(Comparator.comparing(Files::getLastModifiedTime).reversed())
            .findFirst().orElse(null);
    }
}
fge
  • 119,121
  • 33
  • 254
  • 329