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 :(