Results 1 to 2 of 2
  1. #1
    Abder-Rahman is offline Member
    Join Date
    Oct 2008
    Posts
    32
    Rep Power
    0

    Default How can I fix this exception?

    I'm trying to read and display an image (Flower.jpg) and get the following ERROR:


    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:161)
    at ReadDisplayImage.main(ReadDisplayImage.java:26)




    Provided that this is the program:


    //Source: //http://www.exampledepot.com/egs/javax.imageio/BasicImageRead.html
    import java.awt.BorderLayout;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class ReadDisplayImage extends JPanel {
    public static void main(String[] args){

    Image flower = null;

    try {

    File file = new File("Flower.jpg");
    flower = ImageIO.read(file);
    }

    catch (IOException e) {}

    JFrame frame = new JFrame();
    JLabel label = new JLabel(new ImageIcon(flower));
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    }
    }


    Why am I getting this error?

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You may never know why you're getting errors if you keep handling errors like so:

    Java Code:
    try {
    
    File file = new File("Flower.jpg");
    flower = ImageIO.read(file);
    }
    
    catch (IOException e) {}  // !!! ouch !!!
    at least do a print stack trace in the catch block.

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  2. Exception
    By sreejit in forum Advanced Java
    Replies: 2
    Last Post: 10-21-2009, 08:26 AM
  3. exception
    By Oktam in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 07:01 PM
  4. Replies: 1
    Last Post: 03-08-2008, 06:41 PM
  5. Exception
    By Camden in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 11:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •