检查文件是否存在的线程抛出NullPointerException

时间:2017-01-28 05:36:26

标签: java multithreading nullpointerexception

我有一个线程可以在进行任何计算之前检查文件是否存在。

FileSystem fs = ...
Path filenamePath = new Path(file.getID() + "file.txt");
try {
     while(!fs.exists(filenamePath)){
         Thread.sleep(1000);
     }
 } catch (InterruptedException e){
   }

问题是我的线程抛出错误NulPointerException并且永远不会被中断。由于null filenamePathnull,因此ERROR [CheckFilesThread] compute.files.app.FirstApp: Exception in CheckFilesThread java.lang.NullPointerException at compute.files.app.FirstApp$CheckFilesThread.run(FirstApp.java:211) 例外。在这种情况下我该怎么办?我做错了吗?

除了显示行

之外,堆栈跟踪不会显示任何有用的内容
{{1}}

3 个答案:

答案 0 :(得分:0)

  

null异常是因为filenamePath为null。

没有。否则,堆栈跟踪将包括FileSystem.exists()fs为空,因为您显然无法将其初始化为其他任何内容。

否则这不是真正的代码,在这种情况下,你只是在浪费每个人的时间,这是不受欢迎的。

答案 1 :(得分:-1)

我认为使用Watcher是一个更好的主意:

Path dir = ...;
try {
    WatchKey key = dir.register(watcher,
                           ENTRY_CREATE,
                           ENTRY_DELETE,
                           ENTRY_MODIFY);
} catch (IOException x) {
    System.err.println(x);
}

您可以在创建文件时触发线程。这里还有一些documentation

答案 2 :(得分:-1)

     @SuppressWarnings("unchecked")
        public static void watchDirectoryPath(Path path,String fileToWatch) {

            try {
                Boolean isFolder = (Boolean) Files.getAttribute(path, "basic:isDirectory", NOFOLLOW_LINKS);
                if (!isFolder) {
                    throw new IllegalArgumentException("Path: " + path + " is not a folder");
                }
            } catch (IOException ioe) {

                ioe.printStackTrace();
            }

            System.out.println("Watching path: " + path);

            // We obtain the file system of the Path
            FileSystem fs = path.getFileSystem();

            // We create the new WatchService using the new try() block
            try (WatchService service = fs.newWatchService()) {

                // We register the path to the service
                // We watch for creation events
                path.register(service,StandardWatchEventKinds.ENTRY_CREATE);

                // Start the infinite polling loop
                WatchKey key = null;
                while (true) {
                    Path newPath=null;
                    key = service.take();

                    // Dequeueing events
                    Kind<?> kind = null;
                    for (WatchEvent<?> watchEvent : key.pollEvents()) {
                        // Get the type of the event
                        kind = watchEvent.kind();
                        if (StandardWatchEventKinds.OVERFLOW== kind) {
                            continue; // loop
                        } else if(StandardWatchEventKinds.ENTRY_CREATE== kind) {
                            // A new Path was created
                             newPath = ((WatchEvent<Path>) watchEvent).context();

                        }
                    }

                    if (!key.reset() ||  newPath.toString().equals(fileToWatch)) {
                        break; // loop
                    }
                }

            } catch (IOException ioe) {
                ioe.printStackTrace();
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }

        }

    Path folder = Paths.get..
    watchDirectoryPath(folder,"file.txt");