最近在使用URL获取图片资源时,老是出现空指针异常,找了挺久,发现解决办法就``是将图片的格式改为png格式或jfif格式就好了。
例如我成功的代码为:
public static URL headURL=Data.class.getResource("resourse/head.png");
public static ImageIcon head=new ImageIcon(headURL);
而报空指针异常的代码为:
public static URL headURL=Data.class.getResource("resourse/head.jpg");
public static ImageIcon head=new ImageIcon(headURL);