图标图片“error java.lang.NullPointerException”

时间:2012-12-08 15:59:02

标签: java image icons

好吧,我正在尝试设置我的图标图像,并且我一直收到此错误....

代码:

public static void browser(){
        Image myicon = null;
        myicon.equals("images/icon.gif");
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (InstantiationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (UnsupportedLookAndFeelException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        JFrame frame = new JFrame("Kloke - Become invisable.");
        frame.setSize(350, 100);
        frame.setResizable(false);
        frame.setIconImage(myicon);

错误:

Exception in thread "main" java.lang.NullPointerException
        at web.browser(web.java:14)
        at Checkupdate.main(Checkupdate.java:102)

PS。我是java的新手,所以请随意对待我,好像我不知道我在做什么(PSS第14行是我初始化'myicon'变量的地方。)

2 个答案:

答案 0 :(得分:2)

尝试在尚未实例化的对象上调用任何实例方法将导致NullPointerException。您需要实例化myicon

Image myicon = ImageIO.read(new File("images/icon.gif"));

答案 1 :(得分:0)

问题在这里Image myicon = null; myicon.equals("images/icon.gif"); 您正在将myicon设置为null并进行比较,因此编译器会抛出NullPointerException