Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Program to open and display an image file using JFileChooser-Query

  1. #1
    pishu is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Thumbs up Program to open and display an image file using JFileChooser-Query

    HI friends,

    I am getting below runtime exception and i have spent many hours correcting it but didn't suceed (Compiled in netbeans). Need your quick guidance.
    For reference, see selected code below or PFA for full code.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at filechooser.Fchoose.jButton1ActionPerformed(Fchoos e.java:129)
    at filechooser.Fchoose.access$000(Fchoose.java:24)
    at filechooser.Fchoose$1.actionPerformed(Fchoose.java :65)

    Java Code:
     public class Fchoose extends javax.swing.JFrame {
    
     JFileChooser fileChooser; // line 24
    
        public Fchoose() {
            initComponents();
            // fileChooser = new JFileChooser();
        }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            fileChooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "JPG & GIF Images", "jpg", "gif");
            fileChooser.setFileFilter(filter);
            fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
            int result = fileChooser.showOpenDialog(this);
            if (result == JFileChooser.APPROVE_OPTION) {
                // user selects a file
                File selectedFile = fileChooser.getSelectedFile();
                jTextField1.setText(selectedFile.getAbsolutePath());
                System.out.println("Selected file: " + selectedFile.getAbsolutePath());
     try {
                    BufferedImage bf = ImageIO.read(selectedFile);
                    System.out.println(bf);
                    BufferedImage newImage = new BufferedImage(bf.getWidth(), bf.getHeight(), BufferedImage.TYPE_INT_ARGB);
                   System.out.println(newImage);
                   System.out.println("runned0");
                  ImageIcon imageIcon = new ImageIcon(newImage); // i think error is here :)
                    jlabel1.setIcon(imageIcon); // line 129
    
                       System.out.println("runned");
                    
                    System.out.println(new ImageIcon(bf));
                    jlabel1.setVisible(true);
                    jFrame1.getContentPane().add(jlabel1, BorderLayout.CENTER);
                    jFrame1.pack();
                    jFrame1.setLocationRelativeTo(null);
                    jFrame1.setVisible(true);
                    System.out.println("result");
                } catch (IOException ex) {
                    Logger.getLogger(Fchoose.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    
        }       
    public static void main(String args[]) {
          
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Fchoose.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Fchoose.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Fchoose.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Fchoose.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
           
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Fchoose().setVisible(true);
    
                }
            });
    
        }
    Attached Files Attached Files

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    753
    Rep Power
    2

    Default Re: Program to open and display an image file using JFileChooser-Query

    Read the excepyion: NPE in line 129. The only object that is eligable for a NPE is jlabel1. Try to find the line where you say: JLabel jlabel1 = new JLabel(). Can't find it? True, because it isn't there. So there is your NullPointerExpcetion, the object is still NULL.
    pishu likes this.

  3. #3
    pishu is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Thumbs up Re: Program to open and display an image file using JFileChooser-Query

    Quote Originally Posted by SurfMan View Post
    Read the excepyion: NPE in line 129. The only object that is eligable for a NPE is jlabel1. Try to find the line where you say: JLabel jlabel1 = new JLabel(). Can't find it? True, because it isn't there. So there is your NullPointerExpcetion, the object is still NULL.

    Thanks a lot, i created an object for Jlabel & NPE is removed.
    Let me work further as still im waiting for the desired output which is still not coming.
    Thanks once again.:)

  4. #4
    pishu is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Re: Program to open and display an image file using JFileChooser-Query

    I tried a lot, but on running the program , i m getting the new Jlabel created, but the image I selected is not displayed. What i m missing?

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

    Default Re: Program to open and display an image file using JFileChooser-Query

    I see you creating two BufferedImages, one which holds the image of interest obtained from the file, one that doesn't. It looks like you're displaying only the one that doesn't.

Similar Threads

  1. Trouble drawing image from file picked by JFileChooser
    By I = new NewToJava() in forum New To Java
    Replies: 6
    Last Post: 12-26-2010, 03:54 PM
  2. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  3. how to display image selected by JFileChooser
    By khushi.cutegal in forum AWT / Swing
    Replies: 17
    Last Post: 07-24-2010, 06:03 AM
  4. How to display image in a panel with JFileChooser
    By srisar in forum AWT / Swing
    Replies: 14
    Last Post: 10-11-2009, 06:06 PM
  5. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 09:57 AM

Tags for this Thread

Posting Permissions

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