1

I have a thread that will check if a file exists before doing any computation.

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

The problem is that my thread throws the error NulPointerException and never gets interrupted. The null exception becasue filenamePath is null. What should I do in this case? Am I doing something wrong?

The stack trace doesn't show anything useful except showing the line

ERROR [CheckFilesThread] compute.files.app.FirstApp: Exception in CheckFilesThread
java.lang.NullPointerException
    at compute.files.app.FirstApp$CheckFilesThread.run(FirstApp.java:211)
Self
  • 147
  • 2
  • 16
  • *What should I do in this case?* - specify a correct file path – Naman Jan 28 '17 at 05:38
  • @nullpointer The file will be created by different thread if a condition meets. The above thread will do some computation if `file.txt` is created. – Self Jan 28 '17 at 05:39
  • @DavidWallace Yes. Edited. No. This is not the problem. – Self Jan 28 '17 at 05:40
  • 2
    Can you show us the stack trace? – Warren Dew Jan 28 '17 at 05:42
  • 1
    OK, well I don't believe `filenamePath` is null, because you're explicitly setting it to a new object. What about `fs` though? How are you creating it? – Dawood ibn Kareem Jan 28 '17 at 05:42
  • @DavidWallace The error specified the line of `filenamePath`. Note that `filenamePath` is `null` until the file is created by different thread. – Self Jan 28 '17 at 05:47
  • Are you saying that the NullPointerException is thrown on the second line that you've shown here? OK, then what `Path` class is this? The `Path` type in the `nio` package is just an interface, so you wouldn't be creating it like this. – Dawood ibn Kareem Jan 28 '17 at 05:50
  • Obviously `fs` is null, as the stack trace will show when and if you post it in your question. – user207421 Jan 28 '17 at 06:40
  • 1
    Still waiting for the stack trace. And why do you think that `filenamePath` is null? I don't see how it could be. – Dawood ibn Kareem Jan 28 '17 at 06:53
  • @DavidWallace This is my `Path` class [link](https://hadoop.apache.org/docs/stable/api/org/apache/hadoop/fs/Path.html). – Self Jan 28 '17 at 21:51
  • 1
    Great, that makes slightly more sense. Now can you show the stack trace please? – Dawood ibn Kareem Jan 28 '17 at 21:57
  • @DavidWallace the stack trace is added above. Note that this exception keeps showing without effecting the whole process. – Self Jan 28 '17 at 21:58
  • @DavidWallace Don't you think `while(filenamePath == null)` will solve the problem? – Self Jan 28 '17 at 22:12
  • 1
    No, because I still don't believe that `filenamePath` being null is the problem. But if you think it might solve the problem, then by all means go ahead and try it. Now, is line 211 of FirstApp.java the line `Path filenamePath = new Path("file.txt");` or some other line? – Dawood ibn Kareem Jan 28 '17 at 22:25
  • @DavidWallace Yes, it's the same line – Self Jan 28 '17 at 22:30
  • OK, I don't believe that it's possible for that line of code to create the exception shown in your stack trace. Have you tried cleaning out the directories that have your class files, and recompiling your source? It could be possible that you're running an earlier version of the code. – Dawood ibn Kareem Jan 28 '17 at 22:37
  • Either the line in question is not the one you indicated, or else this is not the real code. – user207421 Jan 28 '17 at 23:26
  • @DavidWallace Sorry for the mistake. There is a method as part of the file name `Path filenamePath = new Path(file.getID() + "file.txt");`. I found the `file.getID()` method returned `null` – Self Jan 30 '17 at 23:10
  • @EJP There was a mistake. – Self Jan 30 '17 at 23:11
  • 1
    Yes, if you're asking for help with your code, it's really good if you show your code, rather than showing something slightly different. If you don't take sufficient care when you post a question on Stack Overflow, you just waste everyone's time. – Dawood ibn Kareem Jan 30 '17 at 23:44
  • @DavidWallace This the problem when you don't copy and paste! I typed my code. – Self Jan 31 '17 at 00:12
  • 1
    No, the problem isn't to do with copy and paste. This was the line of code that the stack trace pointed it out, and you didn't bother to look at it, and check that you'd typed it correctly. This is a problem of basic disrespect towards the volunteers on Stack Overflow who would otherwise be perfectly happy to help you out. – Dawood ibn Kareem Feb 01 '17 at 04:32
  • @DavidWallace Yes. This is the line of code that the stack trace pointed it out `Path filenamePath = new Path(file.getID() + "file.txt");`. You just need to believe in this case. :) – Self Feb 01 '17 at 04:39

3 Answers3

1

The null exception is because filenamePath is null.

No. Otherwise the stack trace would include FileSystem.exists(). It is fs that is null, or file, or file.getID().

Or else this isn't the real code, in which case you are just wasting everyone's time.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • As I mentioned to you, there was a mistake. The `null` because `file.getID()` was `null`. Thank you anyway! – Self Feb 01 '17 at 04:52
-1

I think using a Watcher is a better idea:

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

You can trigger your thread when a file is created. Here is some more documentation

Milad
  • 89
  • 1
  • 8
-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");
Hiccup
  • 596
  • 6
  • 19