Java getClass().getResource(“file”) leads to NullPointerException

后端 未结 11 1171
[愿得一人]
[愿得一人] 2020-12-11 01:18

I am following the zetcode Snake java games tutorial and always get this error:

ImageIcon iid = new ImageIcon(this.getClass().getResource(\"ball.png\"));
bal         


        
11条回答
  •  死守一世寂寞
    2020-12-11 01:42

    You can use only path of your image. I think this will help you: Use this:

    ImageIcon iid = new ImageIcon("C:\\Users\\ranig\\My\\spaceinvaders\\ball.png");
    

    Note: C:\\Users\\ranig\\My\\spaceinvaders\\ball.png is the whole path of ball.png image.

    instead of this:

    ImageIcon iid = new ImageIcon(this.getClass().getResource("ball.png"));
    

    Note: If u want to only try snake code and only want to get output.

提交回复
热议问题