我无法真正理解我的程序正在发生的事情。在构建jar文件之前一切正常。运行jar文件时,我得到Exception in thread "main" java.lang.NullPointerException
当我调用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)
重要部分。测试相关部分:
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;
}
我也jar tf RaspberryPi.jar
确认了jar文件中存在key.txt文件。
META-INF/MANIFEST.MF
Client/
Client/key.txt
log4j.properties
Logging.log
META-INF/
RaspberryPiServer/
...
...