4

I am writing a program that requires I have a button with an image over top of it, however, so far, I have not been able to get it to work. I have checked several other posts on this site, including How do I add an image to a JButton.
My Code:

public class Tester extends JFrame
{
    public Tester()
    {
        JPanel panel = new JPanel();
        getContentPane().add(panel);
        panel.setLayout(null);

        setTitle("Image Test");
        setSize(300,300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JButton button = new JButton();
        try 
        {
            Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif"));
            button.setIcon(new ImageIcon(img));
        } 
        catch (IOException ex) {}

        button.setBounds(100,100,100,100);
        panel.add(button);
    }

    public static void main(String[] args)
    {
        Tester test = new Tester();
        test.setVisible(true);
    }
}

When this code runs, an error results: Exception in thread "main" java.lang.IllegalArgumentException: input == null! This error occurs at the line:

Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif"));

I don't think that this error is due to the file not being found by java code, as my Images folder is in the src folder (I am using Eclipse) as recommended by the above link.
Does anyone have any ideas to what the problem may be?
Thanks.

Community
  • 1
  • 1
Tristan Hull
  • 360
  • 1
  • 3
  • 13
  • Please provide image path in your project hierarchy – CAMOBAP Oct 02 '12 at 14:06
  • 1
    Have you actually checked the return value of `getResource()`? – vstm Oct 02 '12 at 14:07
  • This is the image path: C:\Documents and Settings\student\My Documents\Dropbox\ADVCS_Workspace\Chess_Program\src\Images – Tristan Hull Oct 02 '12 at 14:07
  • @TristanHull does not really matter where the image is located. What matter is where the image is located relatively to the corresponding class file. Most likely your image is not found. So first things first: try to see if the image is found or not. Also, don't ever catch exceptions without printing the stacktrace somewhere or leaving a trace of the exception. This makes your code really hard to debug. – Guillaume Polet Oct 02 '12 at 14:12
  • Please have a look at this [answer](http://stackoverflow.com/a/11428289/1057230) of mine related to your problem. And this answer which is related to [HOW TO ADD IMAGES TO YOUR PROJECT](http://stackoverflow.com/a/9866659/1057230) and here is one [working example](http://stackoverflow.com/a/11372350/1057230) with all directory structure of your Project (How it needs to be). – nIcE cOw Oct 02 '12 at 14:14
  • You dont need `getResource()` unless you are going to be dealing with an archive. See my answer below. – davidXYZ Oct 02 '12 at 14:27

5 Answers5

9

While using Eclipse, you don't keep your images into src folder instead you create a Source Folder for this purpose. Please refer to this link regarding how to add images to resource folder in Eclipse.

Community
  • 1
  • 1
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • 1
    +1 nice link :) yup I think it might be that he doesnt have the foward slash – David Kroukamp Oct 02 '12 at 14:18
  • More so the concern for me, is not using the Source FOlder for the said thingy, as the OP said the images are in the src Folder, which I doubt is the right way IMHO – nIcE cOw Oct 02 '12 at 14:19
  • 1
    Resource Folders are mere conventions. You don't have to use a special 'resource folder' for your images to be read. Your path just has to be correct. – davidXYZ Oct 02 '12 at 14:30
  • It sure does, when you get to the task of running your program, whatever is inside your Resource Folder automatically goes to the bin folder. So when you create your .jar file, using Eclipse, you don't have to worry about, whether the path specified will work or not. – nIcE cOw Oct 02 '12 at 14:32
  • Thanks very much I definitely try this. – Tristan Hull Oct 02 '12 at 15:05
  • 1
    That answer was good until the shouting at the end. Now I have a head-ache.. :( – Andrew Thompson Oct 02 '12 at 22:42
2

Use this to create the button.

JButton button = new JButton(new ImageIcon(getClass().getClassLoader()
                                          .getResource("Images/BBishopB.gif")));

And what you are doing is setting the Image as the icon. This doesn't work because the setIcon() method requires objects which implement the Icon interface. Hope this helps.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
  • This will not solve his problem if the image cannot be found. And the second part of your answer is incorrect, since he is reading the image to an Image and then wrapping that Image in an ImageIcon. So that part of the code is correct. – Guillaume Polet Oct 02 '12 at 14:14
  • @GuillaumePolet This part directly creates the ImageIcon from the URL without involving the unnecessary part of the `ImageIO` and others. – Sri Harsha Chilakapati Oct 02 '12 at 14:28
  • You said, and I quote: _This doesn't work because the setIcon() method requires objects which implement the Icon interface_ . That statement is incorrect. – Guillaume Polet Oct 02 '12 at 14:29
  • @GuillaumePolet See this http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setIcon(javax.swing.Icon) This proves my statement – Sri Harsha Chilakapati Oct 02 '12 at 14:32
  • You say "It does not work", that is not true. And anyway, your answer does not solve the problem. – Guillaume Polet Oct 02 '12 at 14:34
  • @GuillaumePolet There's a constructor which accepts `Icon`. And hence `ImageIcon` implements the same interface, it'll work. To know more, see `http://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html#JButton(javax.swing.Icon)` – Sri Harsha Chilakapati Oct 02 '12 at 14:38
  • Forget it. I know everything that you stated. You keep on not reading what I am telling you. – Guillaume Polet Oct 02 '12 at 14:47
2

Try putting a foward slash before the package name in getResource() like so:

Image img = ImageIO.read(getClass().getResource("/Images/BBishopB.gif"));
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
1

You can just find the image directly:

JButton jb = new JButton(new ImageIcon("pic.png")); //pic is in project root folder
//Tip: After placing the image in project folder, refresh the project in Eclipse.

OR if the image will be in a JAR, I usually create a function to do the retrieval for me so that I can re-use it.

public static ImageIcon retrieveIcon(String path){
    java.net.URL imgUrl = 'classpackage'.'classname'.class.getResource(path);
    ImageIcon icon = new ImageIcon(imgUrl);
    return icon;
}

Then I'll do,

JButton jb = new JButton(retrieveIcon("/pic.png"));
davidXYZ
  • 719
  • 1
  • 8
  • 16
1
Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif"));

This line tries to do too much all at one time which makes it difficult to track down an error when you get one. I suggest splitting it up:

URL imgURL = getClass().getResource("Images\\BBishopB.gif");
Image img = ImageIO.read(imgURL);

Now you can use the Eclipse debugger to check the return value of imgURL, which is the most likely candidate for the NPE. Even though this doesn't tell you why you get an error message, it narrows down the problem considerably.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268