您的位置:首頁技術文章
文章詳情頁

Java ImageIO.read(getClass()。getResource())返回null

瀏覽:93日期:2024-04-29 09:22:42
如何解決Java ImageIO.read(getClass()。getResource())返回null?

假設您的課程在package中view.random.name,則

getClass().getResource('gate_and.png')

會在以下位置尋找資源

/view/random/name/gate_and.png

相對于類路徑的根。您顯然那里沒有該名稱的資源。

通過設置project/images為構建路徑條目,Eclipse將在類路徑中包括所有內容。因此,您的資源將顯示在

/gate_and.png

您可以使用

getClass().getResource('/gate_and.png')

請注意 開頭/,這意味著 開始查看類路徑的根 ,即。這是一條絕對的道路。

所有這些規則在javadoc中進行了解釋。

解決方法

andImg = ImageIO.read(getClass().getResource('gate_and.png'));

失敗于

Exception in thread 'AWT-EventQueue-0' java.lang.IllegalArgumentException: input == null!

我正在使用Eclipse,并且在bin文件夾下的導航視圖中有文件gate_and.png,表明該文件位于構建路徑中。

在包瀏覽器視圖中,我有

project/src/view/class - This is the class that has the code above.

project/images/gate_and.png

我右鍵單擊項目文件夾>構建路徑>鏈接源以將images文件夾添加為源,再次執行此操作會提供一條確認消息,指出源中已存在圖像。

我還嘗試過將gate_and.png更改為images /gate_and.png和/images/gate_and.png,但是由于圖像gate_and.png在bin文件夾中,因此我認為原始圖片是正確的。

標簽: java
相關文章:
国产综合久久一区二区三区