Graphics.drawImage()处的空指针异常

时间:2013-02-17 18:22:43

标签: java nullpointerexception java-3d

这是我用来在J3D Canvas3D上绘制图像的代码:

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

但是每当我运行代码时,我都会在g.drawImage()的行中得到一个NullPointerException。我不知道是什么导致了这个例外,我们将不胜感激。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

尝试这样的事情:

图形g = Canvas.getGraphics();

如果(克== NULL){

System.out.println(“没有从getGraphics方法接收任何Graphics对象。”);

}

ImageIcon icon = new ImageIcon(“back / loadingscreen.png”);

如果(图标== NULL){

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

}