2017-03-20 7 views
0

画像、https://spooker.cc/spooker3.pngのBufferedImage /イメージアイコンNULLポインタ例外

コード:

private JLabel getSpookerImage() { 
    URL url; 
    try { 
     url = new URL("http://spooker.cc/spooker3.png"); 
     BufferedImage image = ImageIO.read(url); 
     ImageIcon icon = new ImageIcon(image); 
     return new JLabel(icon, JLabel.CENTER); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 

が例外:おそらく

java.lang.NullPointerException 
at javax.swing.ImageIcon.<init>(ImageIcon.java:240) 
at cc.spooker.ui.tabs.MainTab.getSpookerImage(MainTab.java:63) 
at cc.spooker.ui.tabs.MainTab.getDestructPanel(MainTab.java:53) 
at cc.spooker.ui.tabs.MainTab.<init>(MainTab.java:35) 
at cc.spooker.ui.Frame.<init>(Frame.java:35) 
at cc.spooker.Server.<init>(Server.java:21) 
at cc.spooker.Main.main(Main.java:8) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

画像は空白の背景を持っているので?手掛かりではありません。

+0

どの行が 'Exception'を生成しますか? –

+0

彼はスタックトレースを掲示しました。 –

+0

@AlexR私が知っているのは、どの行が "63' –

答えて

1

ImageIO.Read()メソッドは画像に使用する画像リーダーを探していて、見つからないためnullを返します。 read()メソッドはPNGを受け入れるので、あなたのイメージは有効なPNGファイルではありません。 PNG形式で再エクスポートするか、何らかの形で不正な形式にならないように作成してください。私はここでPNGファイルと同様のコードをテストした:

http://mrose.org/cc/png-test.png

、それが正常に動作します。