Java 检查文件是否存在的线程抛出NullPointerException
我有一个线程,在进行任何计算之前检查文件是否存在Java 检查文件是否存在的线程抛出NullPointerException,java,multithreading,nullpointerexception,Java,Multithreading,Nullpointerexception,我有一个线程,在进行任何计算之前检查文件是否存在 FileSystem fs = ... Path filenamePath = new Path(file.getID() + "file.txt"); try { while(!fs.exists(filenamePath)){ Thread.sleep(1000); } } catch (InterruptedException e){ } 问题是,我的线程抛出错误numpointerExcep
FileSystem fs = ...
Path filenamePath = new Path(file.getID() + "file.txt");
try {
while(!fs.exists(filenamePath)){
Thread.sleep(1000);
}
} catch (InterruptedException e){
}
问题是,我的线程抛出错误numpointerException
,并且从未中断。null
异常是因为filenamePath
是null
。在这种情况下我该怎么办?我做错什么了吗
堆栈跟踪除了显示行之外,没有显示任何有用的内容
ERROR [CheckFilesThread] compute.files.app.FirstApp: Exception in CheckFilesThread
java.lang.NullPointerException
at compute.files.app.FirstApp$CheckFilesThread.run(FirstApp.java:211)
我认为使用观察者是一个更好的主意:
Path dir = ...;
try {
WatchKey key = dir.register(watcher,
ENTRY_CREATE,
ENTRY_DELETE,
ENTRY_MODIFY);
} catch (IOException x) {
System.err.println(x);
}
您可以在创建文件时触发线程。这里还有一些@SuppressWarnings(“未选中”)
公共静态void watchDirectoryPath(路径路径,字符串fileToWatch){
试一试{
Boolean-isFolder=(Boolean)Files.getAttribute(路径,“basic:isDirectory”,NOFOLLOW\u链接);
如果(!isFolder){
抛出新的IllegalArgumentException(“路径:“+Path+”不是文件夹”);
}
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
System.out.println(“监视路径:+path”);
//我们获取路径的文件系统
FileSystem fs=path.getFileSystem();
//我们使用new try()块创建新的WatchService
try(WatchService=fs.newWatchService()){
//我们注册服务的路径
//我们关注创造事件
注册(服务,StandardWatchEventKinds.ENTRY\u创建);
//启动无限轮询循环
WatchKey=null;
while(true){
路径newPath=null;
key=service.take();
//出列事件
种类=空;
for(WatchEvent WatchEvent:key.pollEvents()){
//获取事件的类型
kind=watchEvent.kind();
if(StandardWatchEventKinds.OVERFLOW==kind){
continue;//循环
}else if(StandardWatchEventTypes.ENTRY_CREATE==kind){
//创建了一条新路径
newPath=((WatchEvent)WatchEvent.context();
}
}
如果(!key.reset()| | newPath.toString().equals(fileToWatch)){
中断;//循环
}
}
}捕获(ioe异常ioe){
ioe.printStackTrace();
}捕获(中断异常ie){
即printStackTrace();
}
}
路径文件夹=路径。获取。。
watchDirectoryPath(文件夹,“file.txt”);
null异常是因为filenamePath
为null
否。否则堆栈跟踪将包括FileSystem.exists()
。是fs
为空,或file
或file.getID()
或者这不是真正的代码,在这种情况下,你只是在浪费每个人的时间。你能给我们看看堆栈跟踪吗?好的,我不相信
filenamePath
是空的,因为你显式地将它设置为一个新对象。那么,fs呢?您是如何创建它的?仍在等待堆栈跟踪。为什么您认为filenamePath
为空?我不明白这是怎么回事。太好了,这稍微有点道理。现在可以显示堆栈跟踪了吗?不行,因为我仍然不认为filenamePath
为空是问题所在。但是,如果你认为它可能解决问题,那就千方百计去尝试吧。现在,FirstApp.java的第211行是Path filenamePath=newpath(“file.txt”)代码>或其他行?正如我向您提到的,有一个错误。null
,因为file.getID()
是null
。无论如何谢谢你!
@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");