1
package filefinderusingglobpattern;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;

public class FileFinderUsingGlobPattern {

    public static class Finder extends SimpleFileVisitor<Path> {

        private FileVisitResult CONTINUE;
        private final PathMatcher matcher;
        private int numMatches = 0;

        Finder(String pattern) {
            matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
        }

        // Compares the glob pattern against
        // the file or directory name.
        void find(Path file) {
            Path name = file.getFileName();
            if (name != null && matcher.matches(name)) {
                numMatches++;
                System.out.println(file);
            }
        }

        // Prints the total number of matches to standard out.
        void done() {
            System.out.println("Matched: " + numMatches);
        }

        // Invoke the pattern matching method on each file.
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            find(file);
            return CONTINUE;
        }

        // Invoke the pattern matching method on each directory.
        @Override
        public FileVisitResult preVisitDirectory(Path dir,
                BasicFileAttributes attrs) {
            find(dir);
            return CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) {
            System.err.println(exc);
            return CONTINUE;
        }
    }

    static void usage() {
        System.err.println("java Find <path> -name \"<glob_pattern>\"");
        System.exit(-1);
    }

    public static void main(String[] args) throws IOException {
        /*
        if (args.length < 3 || !args[1].equals("-name")) {
            usage();
        }

        Path startingDir = Paths.get(args[0]);
        String pattern = args[2];

        Finder finder = new Finder(pattern);
        Files.walkFileTree(startingDir, finder);
        finder.done();
         */

        Scanner sc = new Scanner(System.in);
        //System.out.println("C:\\Users\\Ashish Jain\\Desktop\\Test_smsbroadcaster");

        System.out.println("Enter the <path> where you want to look for the files: ");
        String inputPath = sc.nextLine();

        Path startingDir = Paths.get(inputPath);

        if (Files.notExists(startingDir)) {
            System.out.println(startingDir + " does not exist.");
            return;
        } else {
            System.out.println(startingDir + " exists (validation successful).");
        }

        System.out.println("Enter the glob pattern for the files you want to search: ");
        String pattern = sc.nextLine();
        Finder finder = new Finder(pattern);
        Files.walkFileTree(startingDir, finder);
        finder.done();
    }

}

This piece of Java file-finder code from Oracle is throwing null pointer exception in main method, don't know why, please help. It is throwing exception at line 104 "Files.walkFileTree(startingDir, finder);"

Exception in thread "main" java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.nio.file.Files.walkFileTree(Files.java:2699)
at java.nio.file.Files.walkFileTree(Files.java:2742)
at filefinderusingglobpattern.FileFinderUsingGlobPattern.main(FileFinderUsingGlobPattern.java:104)

Check this out: https://docs.oracle.com/javase/tutorial/essential/io/examples/Find.java Unfortunately, this code is erroneous.

Ashish Jain
  • 447
  • 1
  • 6
  • 20

1 Answers1

0
Finder finder = new Finder(pattern);

You're trying to instantiate a static class. You should make Finder non-static if you want to instantiate it with a certain pattern.

AGemmell
  • 19
  • 4
  • Check this out: https://docs.oracle.com/javase/tutorial/essential/io/examples/Find.java – Ashish Jain Jul 21 '17 at 13:39
  • This code works if we remove the line: //private FileVisitResult CONTINUE; Also, change "return CONTINUE;" in the overridden "SimpleFileVisitor" methods to: return FileVisitResult.CONTINUE; – Ashish Jain Jul 21 '17 at 14:39