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

后端 未结 11 1181
[愿得一人]
[愿得一人] 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条回答
  •  -上瘾入骨i
    2020-12-11 01:58

    I will make it simple for you . Here is an example:

    Icon bug = new ImageIcon(getClass().getResource("bug1.png"));
    

    here "bug1.png" is the resource and if it is unavailable then it can cause error as you have discussed here.

    Import an image to the same directory in which your program resides.

    You can also give whole path to it as well

    ImageIcon(getClass().getResource("C://me/file/bug1.png"));
    

提交回复
热议问题