This discussion is archived
4 Replies Latest reply: Mar 28, 2010 6:13 PM by 807580 RSS

ImageIO.read() returns null on second call

807580 Newbie
Currently Being Moderated
I am working on a school project and I have to load images for a game that we are working on. As soon as the applet starts up, it will load a preview of a skin (the game has a customizable interface). This image loads fine. When you go to change the skin and update the preview, or if you start the game, it throws a "java.lang.InvalidArgumentException: input == null!". Even if it is the same image it loaded before, it can't reload it. I don't know what is going on here. It works fine running from NetBeans, but when you run from the JAR file, it will only return a correct image once. I traced it down and it might actually be the classname.class.getResource() that is returning null on the second call; i'm not sure. Can anybody help with this, and soon?
  • 1. Re: ImageIO.read() returns null on second call
    807580 Newbie
    Currently Being Moderated
    You've got a bug somewhere in your code. Perhaps you need to call getClass().getResource("..."), but my guess is that if you want specific help, you'll need to post the offending code. The best type of post is an SSCCE if at all possible. Oh, and you'll want to use code tags to keep your code readable -- please check the forum FAQ for information on this.

    Best of luck and welcome to the Sun forums!
  • 2. Re: ImageIO.read() returns null on second call
    807580 Newbie
    Currently Being Moderated
    /** Creates new form SetupForm */
        public PegGame()
        {
            try
            {
                initComponents();
                URL iconURL = PegGame.class.getResource("images/PegGameIcon.gif");
                Image icon = ImageIO.read(iconURL);
                setIconImage(icon);
                repaint(); //repaint a lot to load image
            }
            catch (IOException ex)
            {
                Logger.getLogger(PegGame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    ^ This image load works ^
    public SkinPreview()
        {
            URL imageURL = SkinPreview.class.getResource(mySkin + "Board.gif");
            JOptionPane.showMessageDialog(this,imageURL);
            try
            {
                i = ImageIO.read(imageURL);
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
            repaint();
            Dimension d = new Dimension(WIDTH, HEIGHT);
            setMinimumSize(d);
            setMaximumSize(d);
            setPreferredSize(d);
            setSize(d);
            repaint();
        }
    ^ And this works too. ^

    As soon as
    skinPreview.setSkin((String) lstSkin.getSelectedItem());
    calls
    /**
         * Sets the SkinPreview's skin.
         * @param skin the skin's name: metal, wood, or sand.
         */
        public void setSkin(String skin)
        {
            if (skin.equalsIgnoreCase("metal") || skin.equalsIgnoreCase("wood") || skin.equalsIgnoreCase("sand"))
            {
                mySkin = skin;
            }
            URL imageURL = SkinPreview.class.getResource(mySkin + "Board.gif");
            JOptionPane.showMessageDialog(this,imageURL);
            try
            {
                i = ImageIO.read(imageURL);
            }
            catch (IOException ex)
            {
                Logger.getLogger(SkinPreview.class.getName()).log(Level.SEVERE, null, ex);
            }
            repaint();
        }
    , it returns a blank URL in
    imageURL
  • 3. Re: ImageIO.read() returns null on second call
    807580 Newbie
    Currently Being Moderated
    Update: I fixed it! The problem was a really stupid mistake that I should have thought of before. When I was getting the selected item from the skin selector, it returned a capitalized name of the skin. The image files start with a lowercase letter. When it runs from NetBeans, it doesn't care about case. When it runs from the jar, however, it requires the file and the filename to be EXACTLY the same case. So this wasn't a bug in Java like I thought, it was just me making another stupid mistake. Thanks for suggesting to upload the code, because as I was looking over it, it got me thinking about if it was a problem with the input to the URL itself.

    Edited by: briman0094 on Mar 28, 2010 4:31 PM
  • 4. Re: ImageIO.read() returns null on second call
    807580 Newbie
    Currently Being Moderated
    briman0094 wrote:
    Update: I fixed it!
    Congratulations! :)
    The problem was a really stupid mistake that I should have thought of before.
    Aren't they always?
    When I was getting the selected item from the skin selector, ...
    And thanks for summarizing the problem and your solution for us. I have a feeling that you will go far in programming. Best of luck!