我试图从Jar的资源文件夹中加载.yml文件。我检索它的行(第33行:input = new FileInputStream(new URL(ClassLoader.getSystemClassLoader().getClass().getClassLoader().getResource("/Resources/devConfig.yml").getFile()).getPath());
)
抛出NullPointerException。
private static void createMap() {
InputStream input = null;
try {
input = new FileInputStream(new URL(ClassLoader.getSystemClassLoader().getClass().getClassLoader().getResource("/Resources/devConfig.yml").getFile()).getPath());
} catch (/*FileNotFoundException*/ Exception e) {
e.printStackTrace();
}
Yaml yaml = new Yaml();
try{
Map l = (Map) yaml.load(input);
}catch(Exception e){
e.printStackTrace();
}
getDevConfig.map = (Map) yaml.load(input);
}
我正在使用snakeYaml库。
在同一个项目中我有这个(几乎)精确的代码来加载Jar外部的YAML文件,唯一的区别是input
加载如下:new FileInputStream(new File("path_to_folder" + filename));
我得到的例外情况:
java.lang.NullPointerException
at util.getDevConfig.createMap(getDevConfig.java:33)
at util.getDevConfig.getKey(getDevConfig.java:48)
org.yaml.snakeyaml.error.YAMLException: java.io.IOException: Stream closed
at org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:200)
at org.yaml.snakeyaml.reader.StreamReader.<init>(StreamReader.java:60)
at org.yaml.snakeyaml.Yaml.load(Yaml.java:412)
at util.getDevConfig.createMap(getDevConfig.java:43)
at util.getDevConfig.getKey(getDevConfig.java:52)
Caused by: java.io.IOException: Stream closed
at java.io.PushbackInputStream.ensureOpen(PushbackInputStream.java:74)
at java.io.PushbackInputStream.read(PushbackInputStream.java:166)
at org.yaml.snakeyaml.reader.UnicodeReader.init(UnicodeReader.java:90)
at org.yaml.snakeyaml.reader.UnicodeReader.read(UnicodeReader.java:122)
at java.io.Reader.read(Reader.java:140)
at org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:184)
我的问题非常简单:为什么我会得到空指针异常?