2013-02-17 88 views
0

這是我使用上繪製一個J3D Canvas3D的圖像的代碼:空指針異常

Graphics g = Canvas.getGraphics(); //Canvas is the Canvas3D 
    g.drawImage(new ImageIcon("back/loadingscreen.png").getImage(), 
      0, 0, Canvas); 
然而

每當我運行代碼我在g線得到一個NullPointerException .drawImage()。我不知道是什麼導致例外,幫助將不勝感激。

+2

你'getGraphics'方法返回'null'。在這兩行之間打印'g'的值以查看它打印的內容。 – 2013-02-17 18:24:40

回答

1

你的ImageIcon爲空 請使用此加載您的ImageIcon正確

InputStream stream = this.getClass().getClassLoader().getResourceAsStream("back/loadingscreen.png"); 
    BufferedImage bufferedImage=ImageIO.read(stream); 
    ImageIcon icon= new ImageIcon(bufferedImage); 

    Graphics g = Canvas.getGraphics(); 
     g.drawImage(icon.getImage(), 0, 0, Canvas); 
+0

它的作品謝謝! – hubbardh 2013-02-17 18:32:31

0

嘗試是這樣的:

圖形G = Canvas.getGraphics();

如果(克== NULL){

的System.out.println( 「未收到任何圖形從方法的getGraphics對象。」);

}

ImageIcon的圖標=新的ImageIcon( 「回/ loadingscreen.png」);

如果(圖標== NULL){

System.out.println("The icon is not received meaning the URL path (back/laodingscreen.png) is probably wrong."); 

}