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.

6
  • Please provide image path in your project hierarchy
    – CAMOBAP
    Oct 2, 2012 at 14:06
  • 1
    Have you actually checked the return value of getResource()?
    – vstm
    Oct 2, 2012 at 14:07
  • This is the image path: C:\Documents and Settings\student\My Documents\Dropbox\ADVCS_Workspace\Chess_Program\src\Images Oct 2, 2012 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. Oct 2, 2012 at 14:12
  • Please have a look at this answer of mine related to your problem. And this answer which is related to HOW TO ADD IMAGES TO YOUR PROJECT and here is one working example with all directory structure of your Project (How it needs to be).
    – nIcE cOw
    Oct 2, 2012 at 14:14

5 Answers 5

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.

6
  • 1
    +1 nice link :) yup I think it might be that he doesnt have the foward slash Oct 2, 2012 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 2, 2012 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 2, 2012 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 2, 2012 at 14:32
  • 1
    That answer was good until the shouting at the end. Now I have a head-ache.. :( Oct 2, 2012 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.

7
  • 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. Oct 2, 2012 at 14:14
  • @GuillaumePolet This part directly creates the ImageIcon from the URL without involving the unnecessary part of the ImageIO and others. Oct 2, 2012 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. Oct 2, 2012 at 14:29
  • @GuillaumePolet See this docs.oracle.com/javase/7/docs/api/javax/swing/… This proves my statement Oct 2, 2012 at 14:32
  • You say "It does not work", that is not true. And anyway, your answer does not solve the problem. Oct 2, 2012 at 14:34
2

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

Image img = ImageIO.read(getClass().getResource("/Images/BBishopB.gif"));
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"));
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.