0

I can't really wrap my head around what's going on with my program. Everything works fine before building the jar file. When running the jar file I get Exception in thread "main" java.lang.NullPointerException

The NullPointerException happens when I call System.out.println(FileHandler.readInputStream(path));

Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Unknown Source)
    at java.io.InputStreamReader.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at RaspberryPiServer.FileHandler.FileHandler.readInputStream(FileHandler.java:17)
    at RaspberryPiServer.Client.<init>(Client.java:47)
    at RaspberryPiServer.Main.main(Main.java:277)

The important parts. Testing the relevant parts:

String path = "Client\\key.txt";
System.out.println(FileHandler.readInputStream(path));

// RaspberryPiServer.FileHandler class:
public static String readInputStream(String path) {
    InputStream input = RaspberryPiServer.Utils.ResourceLoader.loader(path);
    java.util.Scanner s = new java.util.Scanner(input);
    s.useDelimiter("\\A");
    String streamString = s.hasNext() ? s.next() : "";
    s.close();

    return streamString;
}

// RaspberryPiServer.Utils.ResourceLoader class
public static InputStream loader(String path) {

    InputStream input = ResourceLoader.class.getResourceAsStream(path);

    if (input == null) {
        input = ResourceLoader.class.getResourceAsStream("/" + path);
    }

    return input;
}

I also did jar tf RaspberryPi.jar to confirm that the key.txt file exists within the jar file.

META-INF/MANIFEST.MF
Client/
Client/key.txt
log4j.properties
Logging.log
META-INF/
RaspberryPiServer/
...
...
patski
  • 329
  • 6
  • 15
  • Even if this would not boil down to: learn and understand what an NPE is - the question would still be incomplete. You forget the stack trace - or at least to point in **which** line the NPE occurs. – GhostCat Aug 24 '17 at 12:05
  • No point in marking this a duplicate as this does not boil down to: learn and understand what an NPE is - as said by yourself.... – patski Aug 24 '17 at 12:09
  • Well. Your question does not contain the information that we would need to help. That somehow implies that you do not know enough about NPEs. – GhostCat Aug 24 '17 at 12:10
  • Stacktrace added. Any idea why the program runs fine before building a jar file? – patski Aug 24 '17 at 12:14
  • Hint: you want to mark the lines in the source code which the stack trace is pointing to. – GhostCat Aug 24 '17 at 12:15
  • Do you have more luck with `"Client/key.txt"` ? – Arnaud Aug 24 '17 at 12:16
  • 1
    That made everything work again. Thanks @Berger – patski Aug 24 '17 at 12:21
  • You're welcome, and here is the relevant piece of javadoc from https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String) : "The name of a resource is a '/'-separated path name that identifies the resource." – Arnaud Aug 24 '17 at 12:27

0 Answers0