我正在尝试读取将在 jar 包中的文件 (blip3.out)。我正在使用 getResourceAsStream 获取 url,然后尝试从中读取。我已经多次尝试,使用其他帖子的解决方案,但我仍然收到ExceptionInInitializerError。有人可以解释我做错了什么或问题可能是什么。
此外,据我了解,如果我能让 get 资源流正常工作,那么它应该将 blip3.out 文件包含到 jar 包中。这个对吗?
代码:
public Set<String> readWindowTitleSet() {
try {
InputStream is = this.getClass().getResourceAsStream("blip3.out");
ObjectInputStream ois = new ObjectInputStream(is);
anotherList = (HashSet<String>) ois.readObject();
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return anotherList;
}
错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at view.MainGui.<init>(MainGui.java:29)
at view.MainGui.main(MainGui.java:38)
Caused by: java.lang.NullPointerException
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at model.WindowTitleDataStore.readWindowTitleSet(WindowTitleDataStore.java:47)
at model.WindowTitleDataStore.<init>(WindowTitleDataStore.java:18)
at controller.InterruptionDecision.<clinit>(InterruptionDecision.java:23)
... 2 more