The exceptions you are getting are happening because you aren't controlling your UI events.
A ClosedWatchServiceException
occurs when you try to use a WatchService
whose close()
method has been called. So you are probably calling watchService.take()
or some other WatchService
method after calling close()
. It might happen that the take()
method unblocks after you close the WatchService
and immediately throws the Exception
.
You get a NullPointerException
with key
because you are trying to call cancel()
on the instance before having initialized it. I'm guessing it's declared somewhere in your class as
private WatchKey key;
By default, instance reference type variables are initialized to null
. If your execution never takes you through
key = watchService.take();
then key
will remain null
.