javaswingnullpointerexceptionawtembedded-resource

Loading resources like images while running project distributed as JAR archive


I am having a error for my GUI. Trying to set title bar icon then be included in a Runnable JAR.

BufferedImage image = null;
try {
    image = ImageIO.read(getClass().getClassLoader().getResource("resources/icon.gif"));
} 
catch (IOException e) {
    e.printStackTrace();
}

frame.setIconImage(image);

Here is the error I am getting:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at GUI.<init>(GUI.java:39)
    at GUI.main(GUI.java:351)

The image is in the correct directory which "resources" folder is the root of the project file


Solution

  • First of all, change this line :

    image = ImageIO.read(getClass().getClassLoader().getResource("resources/icon.gif"));
    

    to this :

    image = ImageIO.read(getClass().getResource("/resources/icon.gif"));
    

    More info, on as to where lies the difference between the two approaches, can be found on this thread - Different ways of loading a Resource

    For Eclipse:

    For NetBeans:

    For IntelliJ IDEA:

    If you are doing it manually :

    QUICK REFERENCE CODE EXAMPLE(though for more detail consider, A little extra clarification link):

    package swingtest;
    
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Gagandeep Bali
     * Date: 7/1/14
     * Time: 9:44 AM
     * To change this template use File | Settings | File Templates.
     */
    public class ImageExample {
    
        private MyPanel contentPane;
    
        private void displayGUI() {
            JFrame frame = new JFrame("Image Example");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            contentPane = new MyPanel();
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private class MyPanel extends JPanel {
    
            private BufferedImage image;
    
            public MyPanel() {
                try {
                    image = ImageIO.read(MyPanel.class.getResource("/resources/images/planetbackground.jpg"));
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
    
            @Override
            public Dimension getPreferredSize() {
                return image == null ? new Dimension(400, 300): new Dimension(image.getWidth(), image.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, this);
            }
        }
    
        public static void main(String[] args) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    new ImageExample().displayGUI();
                }
            };
            EventQueue.invokeLater(runnable);
        }
    }