简体   繁体   English

ExceptionInInitializerError - 使用getResourceAsStream读取jar包中的文件

[英]ExceptionInInitializerError - using getResourceAsStream to read a file in a jar package

I'm trying to read a file (blip3.out) that will be in a jar package. 我正在尝试读取将在jar包中的文件(blip3.out)。 I am using getResourceAsStream to get the url and and then try to read from it. 我使用getResourceAsStream来获取url,然后尝试从中读取。 I've made multiple attempts, with solutions from other posts, but I am still getting an ExceptionInInitializerError . 我使用其他帖子的解决方案进行了多次尝试,但我仍然得到ExceptionInInitializerError Could someone please explain what I'm doing wrong or what the issue might be. 有人可以解释我做错了什么或问题可能是什么。

Also, from what I understand if I can get the get resource stream working then it should include the blip3.out file to the jar package. 另外,根据我的理解,如果我可以获得get资源流,那么它应该将blip3.out文件包含在jar包中。 Is this correct? 这个对吗?

Code: 码:

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;

}

Error: 错误:

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

The cause of the error is simple: you actually pass null as an inputstream (looking at the sources of PeekInputStream#read shows that there's no other reason for that). 错误的原因很简单:您实际上将null作为输入流传递(查看PeekInputStream#read的源代码PeekInputStream#read显示没有其他原因)。 Add a null check and move the file to the correct location, because the application can't find it there at runtime. 添加空检查并将文件移动到正确的位置,因为应用程序无法在运行时找到它。

(Guessing not knowing: place it in the "view" folder in your sources directory) (猜测不知道:将它放在sources目录的“view”文件夹中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM