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

后端 未结 11 1188
[愿得一人]
[愿得一人] 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:59

    It is general risky to load resources using relative paths, I'd always recommend using absolute paths, so do

     /ball.png
    

    if the the image is at the root of your classpath, or add a path to the location.

提交回复
热议问题