2016-10-17 25 views
0

一つは画像ロードする(InteliJ対日食):
プロジェクト> RES> img.png(パス= "RES/img.png")読み込み画像

BufferedImage image = loadImage(path); 

LoadImageである:

Eclipseを使用して
protected BufferedImage loadImage(String path) { 
    BufferedImage img = null; 
    try { 
     img = ImageIO.read(new File(path)); 
    } catch(IOException e) { 
     System.err.println("could not load: " + path); 
    } 
    return img; 
} 


誰かが使用される:
(パス= "/img.png")

BufferedImage image = null; 
    try { 
     image = ImageIO.read(Sprite.class.getResourceAsStream(path)); 
    } catch (IOException e) {  
     e.printStackTrace(); 
    } 

しかしのIntelliJでこれを使用してはいます: 入力== nullの:スレッド "Game_main" java.lang.IllegalArgumentExceptionがで

例外を!

なぜgetResourceAsStreamに失敗しますか?

答えて

0

getResourceAsStream()は、(デフォルトで)システムクラスローダーを使用してファイルを検索します。したがって、resourcesディレクトリはクラスパス上になければなりません。IntelliJプロジェクトにresディレクトリが正しく含まれていることを確認し、リソースディレクトリとしてマークします。