2017-09-02 71 views
1

我有一個圖像轉換爲緩衝的圖像Java BufferedImage加載不工作|與該路徑問題

public static BufferedImage imageLoader(String path) 

加載圖像時,它返回一個方法..

return ImageIO.read(ImageLoader.class.getResource(path)); 

所以當我嘗試加載和顯示在圖像我的畫布我做..

testImage = ImageLoad.imageLoader("textures/sky.png"); 

確定,所以什麼即時得到的是事實,這並不正確加載和IM試圖找出爲什麼,當我加載這裏是發生了什麼......

Exception in thread "main" java.lang.IllegalArgumentException: 
    input==null! 
    at javax.imageio.ImageIO.read(ImageIO.java:1388) 
    at gfx.ImageManager.ImageLoad.imageLoader(ImageLoad.java:14) 
    at game.Thread.GameThread.init(GameThread.java:119) 
    at game.Thread.GameThread.run(GameThread.java:30) 
    at game.Thread.GameThread.startThread(GameThread.java:50) 
    at launcher.Launcher.main(Launcher.java:15) 

所以基本上沒有正確加載,原因IM將所有這些信息,是因爲我已經問了好幾個人,他們不能弄明白。所以我想看看我是否做錯了什麼,或者如果有更簡單的解決方案。這裏是我的項目的src文件夾...

我使用IntelliJ,如果有幫助。

+0

圖像存儲在哪裏?你能提供給你的問題而不是鬆散的線條的功能嗎? – pedromss

+0

它存儲在一個單獨的包,但它仍然在同一根。 – MegaLunchBox

回答

1

ImageIO.read()

* @exception IllegalArgumentException if <code>input</code> is 
* <code>null</code>. 
* @exception IOException if an error occurs during reading. 
*/ 
public static BufferedImage read(File input) throws IOException { 
    if (input == null) { 
     throw new IllegalArgumentException("input == null!"); 
    } 

執行情況的片斷應該確認要傳遞給該方法的值是null幫你調試的原因可能ImageLoader.class.getResource(path)可能/是null爲共享代碼上方。

由於它依賴於像pathImageLoader這樣的其他幾個變量的定義,所以我會將它留給你做一些調試並對其進行分類。

+0

建議他將圖像放在項目根目錄下,並用'ImageIO.read(文件輸入)'作爲'File'讀取。閱讀資源文件對於新手來說可能會很棘手。 –

+1

我想去嘗試那個名字我不能類型的傢伙 – MegaLunchBox

+0

@КлаусШварц這就是我把它留下的地方*我將它留在你身上做一些調試並將其整理出來* – nullpointer