Airis
Airis

Reputation: 191

Icon image "error java.lang.NullPointerException"

Well, I'm trying to set up my icon image, and I keep getting this error....

Code:

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);

Error:

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

PS. I am new to java, so feel free to treat me as if I have no idea what I am doing(PSS Line 14 is where I initialize the 'myicon' variable.)

Upvotes: 0

Views: 2176

Answers (2)

Pradeep Simha
Pradeep Simha

Reputation: 18123

Problem is here Image myicon = null; myicon.equals("images/icon.gif"); You are setting myicon to null and comparing, so compiler is throwing NullPointerException

Upvotes: 0

Reimeus
Reimeus

Reputation: 159784

Attempting to call any instance method on an object that has not been instantiated will cause a NullPointerException. You need to instantiate myicon

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

Upvotes: 2

Related Questions