Java: NullPointerException from class.getResource( … )

后端 未结 5 1870
北海茫月
北海茫月 2020-11-30 14:03

I was writing a small application and when I tried to create an ImageIcon I always got an exception. The exception was caused by this line of code:

prayerLev         


        
5条回答
  •  借酒劲吻你
    2020-11-30 14:43

    I believe the NPE is being thrown from the ImageIcon constructor as getResource is returning null.

    Try the following:

    getClass().getClassLoader().getResource("/icons/icon_prayer.png")
    

    Or:

    ClassLoader.getSystemResource("/icons/icon_prayer.png")
    

提交回复
热议问题