0

First I used JLabel and ImageIcon. It worked fine. However, the image doesn't show up when I exported it to JAR.
So, then I created my own ImagePanel and used the drawImage method to display it. But it terminates and throws an

exception in thread "main" java.lang.IllegalArgumentException: input == null!

well, my code follows below..

startMenu

public class startMenu extends JFrame{
  ...
  URL imgUrl = getClass().getResource("images/contacts.png");

  public startMenu(){
    ...
    ImagePanel imgPanel = new ImagePanel(imgUrl);
  }

}

ImagePanel

public class ImagePanel extends JPanel{

    private Image image;
    private URL imgUrl;

 public ImagePanel(URL url){
        this.imgUrl= url;

        try{    
            image = ImageIO.read(imgUrl);
        } 
        catch (IOException e) {
            System.out.println("no such file");
        }
    }

 @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(image!=null){
             g.drawImage(image, 10, 12, 128, 128, this);
        }
        else{
            g.drawImage(image, 0, 0, this);
        }
    }

    @Override
    public Dimension getPreferredSize() {
       return new Dimension(150,150);
    }
}

If I would use ImageIcon with an imageUrl. It shows the same error message.

  URL imgUrl = getClass().getResource("images/contacts.png");
    private ImageIcon adBook = new ImageIcon(imgUrl);
    private JLabel mainImg = new JLabel(adBook);
kk-dev11
  • 2,654
  • 5
  • 37
  • 48
  • Are you sure your image is in your jar in a directory named `"images"`? (since you load it with `getClass().getResource("images/contacts.png");`) – ulmangt Apr 15 '12 at 17:51
  • `"images/contacts.png"` Put a leading `/`, e.g. `"/images/contacts.png"` – Andrew Thompson Apr 15 '12 at 17:55
  • 2
    *"First I used JLabel and ImageIcon. It worked fine. However, the image doesn't show up when I exported it to JAR."* That must be because you were supplying a `String` to the constructor. There is also a constructor for `URL`. The problem then becomes locating the image, which is the same for either way of doing it. Change back to using a `JLabel` to display the image. – Andrew Thompson Apr 15 '12 at 17:57
  • 2
    Copy/paste the exception output, rather than put a screen-shot! Screen-shots take more bandwidth, and the text is not available to search on. – Andrew Thompson Apr 15 '12 at 17:59
  • @ulmangt When I used ImageIcon("images/contacts.png") and exported to JAR, it didn't have images directory. But now with ImageIO, it doesn't even run in eclipse. – kk-dev11 Apr 15 '12 at 18:03
  • @AndrewThompson But I can't add an Image to JLabel. So you mean ImageIcon i = new ImageIcon(imageUrl) ?? – kk-dev11 Apr 15 '12 at 18:07
  • @AndrewThompson I've tried with JLabel, as you said. The code is right at the bottom. But I'm afraid, the same error message shows up. – kk-dev11 Apr 15 '12 at 18:16
  • As I mentioned before (but a little louder this time). **Put a leading `/`** – Andrew Thompson Apr 15 '12 at 18:17
  • leading / didn't help either. I've already tried it. – kk-dev11 Apr 15 '12 at 18:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10097/discussion-between-freetalk13-and-andrew-thompson) – kk-dev11 Apr 15 '12 at 18:20
  • Have you tried using `getResourceAsStream()` instead of `getResource()`? Also, as sometimes the location can change (as AndrewThompson has mentioned) when you've packaged images into a Jar. Have you tried the following? `"src/images/contacts.png"` and `"/src/images/contacts.png"` (or whatever the full path from the workspace project directory to the images directory) – BenCole Apr 15 '12 at 18:22
  • getResourceAsStream, it's not any more URL type. It changes to InputStream. Now I can't give this as parameter to ImageIcon. :( InputStream imgUrl = getClass().getResourceAsStream("/src/images/contacts.png"); – kk-dev11 Apr 15 '12 at 18:25
  • thanks buddy. I just tried to export to JAR with the ImageIcon(URL). It works. I should use "getClass().getResource("images/contacts.png");" but it still won't work in Eclipse. Though tedious it is, I just need to change back. – kk-dev11 Apr 15 '12 at 18:45

1 Answers1

0

problem is where the Images are located. I always saved the images directly inside the directory where "src" and "bin" are. "but getClass().getResource("...")" returns the path of bin. So I just had to move the image folder into "src" , so it will automatically make a copy of this directory into 'bin'. problem solved.

URL imgUrl = getClass().getResource("images/contacts.png");

returns--> Q:\workspace\project name\bin\images\contacts.png

kk-dev11
  • 2,654
  • 5
  • 37
  • 48