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

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

    if the resource is in your classpath then you should be trying "this.getClass().getClassLoader().getResource("ball.png")". For you actual code to work, the ball.png needs to be in the location where your .class file is (i.e., inside the package).

提交回复
热议问题