7 Replies Latest reply: Aug 14, 2012 4:40 AM by sabre150 RSS

    NullPointerException in JFileChooser.showOpenDialog() JRE 1.7

    843096
      Hi all,

      In my application, i have used JFileChooser to select a file. When i try to open JFileChooser.showOpenDialog() it throws the following exception. Not sure where i am wrong. Please help me.

      Exception:

      #Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      # at sun.awt.shell.Win32ShellFolder2.access$200(Unknown Source)
      # at sun.awt.shell.Win32ShellFolder2$1.call(Unknown Source)
      # at sun.awt.shell.Win32ShellFolder2$1.call(Unknown Source)
      # at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
      # at java.util.concurrent.FutureTask.run(Unknown Source)
      # at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      # at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      # at sun.awt.shell.Win32ShellFolderManager2$ComInvoker$3.run(Unknown Source)
      # at java.lang.Thread.run(Unknown Source)


      Source Code:

           public File browseFile() {
                UIManager.put("FileChooser.readOnly", Boolean.TRUE);
                if (DEBUG > 4) {
                     System.out.println("Initial Dir : " + initialDir);
                     System.out.println("Title : " + title);
                }

                JFileChooser jfc = new JFileChooser();
                FileFilter filter = null;

                if (null != extension && extension.trim().length() > 0) {
                     if (null != description && description.trim().length() > 0) {
                          filter = new ExtensionFilter(this.description, this.extension);
                     } else {
                          filter = new ExtensionFilter(this.extension, this.extension);
                     }
                     jfc.setFileFilter(filter);
                }

                if (null != title && title.trim().length() > 0) {
                     jfc.setDialogTitle(title);
                }

                if (null != initialDir) {
                     jfc.setCurrentDirectory(new File(initialDir));
                }

                int returnVal = - 1;

                if (type == JFileChooser.OPEN_DIALOG) {
                     returnVal = jfc.showOpenDialog(this);
                } else if (type == JFileChooser.SAVE_DIALOG) {
                     returnVal = jfc.showSaveDialog(this);
                }

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                     File file = jfc.getSelectedFile();
                     setVisible(false);
                     return file;
                } else {
                     setVisible(false);
                     return null;
                }

           }

      Thanks for all in Advance.

      /dhavan
        • 1. Re: NullPointerException in JFileChooser.showOpenDialog() JRE 1.7
          sabre150
          A common cause of this sort of exception is when Swing components are create and realised outside of the event thread. Since the exception seems to involve a concurrent process I'm betting that this is the case.
          • 2. Re: NullPointerException in JFileChooser.showOpenDialog() JRE 1.7
            843096
            Thanks for your reply Sabre.

            I made changes to my code where i am trying to invoke the code within EventQueue.invokeLater(). still facing the same issue.


            Modified Code

                 public File browseFile() {
                      parent = this;
                      UIManager.put("FileChooser.readOnly", Boolean.TRUE);
            //          if (DEBUG > 4) {
                           System.out.println("Initial Dir : " + initialDir);
                           System.out.println("Initial File : " + initialFile);
                           System.out.println("Title : " + title);
            //          }

                      try {
                           _EventQueue.invokeLater( new Runnable() {_
                                public void run() {
                                     jfc = new JFileChooser();
                                     FileFilter filter = null;
                                     if (null != extension && extension.trim().length() > 0) {
                                          if (null != description && description.trim().length() > 0) {
                                               filter = new ExtensionFilter(description, extension);
                                          } else {
                                               filter = new ExtensionFilter(extension, extension);
                                          }
                                          jfc.setFileFilter(filter);
                                     }

                                     if (null != title && title.trim().length() > 0) {
                                          jfc.setDialogTitle(title);
                                     }

                                     if (null != initialDir) {
                                          jfc.setCurrentDirectory(new File(initialDir));
                                     }

                                if (type == JFileChooser.OPEN_DIALOG) {
                                     System.out.println("JFileChooser.OPEN_DIALOG");
                                     returnVal = jfc.showOpenDialog(parent);
                                } else if (type == JFileChooser.SAVE_DIALOG) {
                                     System.out.println("JFileChooser.SAVE_DIALOG");
                                     returnVal = jfc.showSaveDialog(parent);
                                }
                                }
                           });
                      } catch (Exception ex) {
                           ex.printStackTrace();
                      }


                      if (returnVal == JFileChooser.APPROVE_OPTION) {
                           File file = jfc.getSelectedFile();
                           return file;
                      } else {
                           return null;
                      }

                 }
            • 3. Re: NullPointerException in JFileChooser.showOpenDialog() JRE 1.7
              sabre150
              Are you sure it's the same problem? You now initialise 'jfc' in the event queue but in doing this
              File file = jfc.getSelectedFile();
              you probably access it before it has been initialised so that it will be null.

              P.S. Please use [ code] tags to make your code readable.
              • 4. Re: NullPointerException in JFileChooser.showOpenDialog() JRE 1.7
                843096
                Sorry, actually i commented that line. Here is the updated code.
                     public File browseFile() {
                          parent = this;
                          UIManager.put("FileChooser.readOnly", Boolean.TRUE);
                //          if (DEBUG > 4) {
                               System.out.println("Initial Dir : " + initialDir);
                               System.out.println("Initial File : " + initialFile);
                               System.out.println("Title : " + title);
                //          }
                
                          try {
                               EventQueue.invokeLater( new Runnable() {
                                    public void run() {
                                         jfc = new JFileChooser();
                                         FileFilter filter = null;
                                         if (null != extension && extension.trim().length() > 0) {
                                              if (null != description && description.trim().length() > 0) {
                                                   filter = new ExtensionFilter(description, extension);
                                              } else {
                                                   filter = new ExtensionFilter(extension, extension);
                                              }
                                              jfc.setFileFilter(filter);
                                         }
                
                                         if (null != title && title.trim().length() > 0) {
                                              jfc.setDialogTitle(title);
                                         }
                
                                         if (null != initialDir) {
                                              jfc.setCurrentDirectory(new File(initialDir));
                                         }
                
                                    if (type == JFileChooser.OPEN_DIALOG) {
                                         System.out.println("JFileChooser.OPEN_DIALOG");
                                         returnVal = jfc.showOpenDialog(parent);
                                    } else if (type == JFileChooser.SAVE_DIALOG) {
                                         System.out.println("JFileChooser.SAVE_DIALOG");
                                         returnVal = jfc.showSaveDialog(parent);
                                    }
                                    }
                               });
                          } catch (Exception ex) {
                               ex.printStackTrace();
                          }
                          return null;
                /*
                          if (returnVal == JFileChooser.APPROVE_OPTION) {
                               File file = jfc.getSelectedFile();
                               return file;
                          } else {
                               return null;
                          }*/
                
                     }
                Exception:-

                #Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
                #     at sun.awt.shell.Win32ShellFolderManager2.createShellFolder(Unknown Source)
                #     at sun.awt.shell.Win32ShellFolderManager2.getRecent(Unknown Source)
                #     at sun.awt.shell.Win32ShellFolderManager2.get(Unknown Source)
                #     at sun.awt.shell.ShellFolder.get(Unknown Source)
                #     at sun.swing.WindowsPlacesBar.<init>(Unknown Source)
                #     at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.updateUseShellFolder(Unknown Source)
                #     at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installComponents(Unknown Source)
                #     at javax.swing.plaf.basic.BasicFileChooserUI.installUI(Unknown Source)
                #     at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installUI(Unknown Source)
                #     at javax.swing.JComponent.setUI(Unknown Source)
                #     at javax.swing.JFileChooser.updateUI(Unknown Source)
                #     at javax.swing.JFileChooser.setup(Unknown Source)
                #     at javax.swing.JFileChooser.<init>(Unknown Source)
                #     at javax.swing.JFileChooser.<init>(Unknown Source)
                #     at com.marimba.ncp.apps.editor.SwingFileChooser$1.run(SwingFileChooser.java:59)
                #     at java.awt.event.InvocationEvent.dispatch(Unknown Source)
                #     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
                #     at java.awt.EventQueue.access$000(Unknown Source)
                #     at java.awt.EventQueue$1.run(Unknown Source)
                #     at java.awt.EventQueue$1.run(Unknown Source)
                #     at java.security.AccessController.doPrivileged(Native Method)
                #     at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
                #     at java.awt.EventQueue.dispatchEvent(Unknown Source)
                #     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
                #     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
                #     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
                #     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                #     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                #     at java.awt.EventDispatchThread.run(Unknown Source)
                • 5. Re: NullPointerException in JFileChooser.showOpenDialog() JRE 1.7
                  sabre150
                  I can see nothing obviously wrong in that code but I can see nothing in the stack trace to indicate that your problem is in that code. Since you now return 'null' regardless are you sure that the exception is not caused by you returning 'null'? You should put some debug statements in that code to see where the NPE is being thrown.

                  P.S. It still does not look like the same problem as you first reported.
                  • 6. Re: NullPointerException in JFileChooser.showOpenDialog() JRE 1.7
                    843096
                    Again modified the code to confirm that the NPE is not because of returning Null.
                    return new File("c:\\test.txt");
                    Still no luck. I have added few debug messages. here is the complete logs with new debug messages.

                    #Initial Dir : C:\Program Files\BMC Software\BBCA\Tuner
                    #Initial File : null
                    #Title : Add File
                    _#(Debug) FileChooser.browseFile(): Initializing JFileChooser()_
                    #[14/Aug/2012:10:34:13 +0530]
                    #Exception occurred during event dispatching:
                    #java.lang.NullPointerException
                    #     at sun.awt.shell.Win32ShellFolderManager2.createShellFolder(Unknown Source)
                    #     at sun.awt.shell.Win32ShellFolderManager2.getRecent(Unknown Source)
                    #     at sun.awt.shell.Win32ShellFolderManager2.get(Unknown Source)
                    #     at sun.awt.shell.ShellFolder.get(Unknown Source)
                    #     at sun.swing.WindowsPlacesBar.<init>(Unknown Source)
                    #     at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.updateUseShellFolder(Unknown Source)
                    #     at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installComponents(Unknown Source)
                    #     at javax.swing.plaf.basic.BasicFileChooserUI.installUI(Unknown Source)
                    #     at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installUI(Unknown Source)
                    #     at javax.swing.JComponent.setUI(Unknown Source)
                    #     at javax.swing.JFileChooser.updateUI(Unknown Source)
                    #     at javax.swing.JFileChooser.setup(Unknown Source)
                    #     at javax.swing.JFileChooser.<init>(Unknown Source)
                    #     at javax.swing.JFileChooser.<init>(Unknown Source)
                    #     at com.marimba.ncp.apps.editor.SwingFileChooser$1.run(SwingFileChooser.java:60)
                    #     at java.awt.event.InvocationEvent.dispatch(Unknown Source)
                    #     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
                    #     at java.awt.EventQueue.access$000(Unknown Source)
                    #     at java.awt.EventQueue$1.run(Unknown Source)
                    #     at java.awt.EventQueue$1.run(Unknown Source)
                    #     at java.security.AccessController.doPrivileged(Native Method)
                    #     at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
                    #     at java.awt.EventQueue.dispatchEvent(Unknown Source)
                    #     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
                    #     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
                    #     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
                    #     at java.awt.Dialog$1.run(Unknown Source)
                    #     at java.awt.Dialog$3.run(Unknown Source)
                    #     at java.security.AccessController.doPrivileged(Native Method)
                    #     at java.awt.Dialog.show(Unknown Source)
                    #     at marimba.gui.PlayerDialog.show(PlayerDialog.java:214)
                    #     at com.marimba.ncp.apps.snapshot.MessageBox.show(MessageBox.java:186)
                    #     at com.marimba.ncp.apps.snapshot.MessageBox.show(MessageBox.java:144)
                    #     at com.marimba.ncp.apps.editor.BasicView.doAddFile(BasicView.java:6179)
                    #     at com.marimba.ncp.apps.editor.BasicView.doCommand(BasicView.java:4615)
                    #     at com.marimba.ncp.apps.editor.ShrinkwrapView.doCommand(ShrinkwrapView.java:674)
                    #     at com.marimba.ncp.apps.editor.BasicView.actionPerformed(BasicView.java:3804)
                    #     at java.awt.MenuItem.processActionEvent(Unknown Source)
                    #     at java.awt.MenuItem.processEvent(Unknown Source)
                    #     at java.awt.MenuComponent.dispatchEventImpl(Unknown Source)
                    #     at java.awt.MenuComponent.dispatchEvent(Unknown Source)
                    #     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
                    #     at java.awt.EventQueue.access$000(Unknown Source)
                    #     at java.awt.EventQueue$1.run(Unknown Source)
                    #     at java.awt.EventQueue$1.run(Unknown Source)
                    #     at java.security.AccessController.doPrivileged(Native Method)
                    #     at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
                    #     at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
                    #     at java.awt.EventQueue$2.run(Unknown Source)
                    #     at java.awt.EventQueue$2.run(Unknown Source)
                    #     at java.security.AccessController.doPrivileged(Native Method)
                    #     at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
                    #     at java.awt.EventQueue.dispatchEvent(Unknown Source)
                    #     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
                    #     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
                    #     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
                    #     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                    #     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                    #     at java.awt.EventDispatchThread.run(Unknown Source)
                    • 7. Re: NullPointerException in JFileChooser.showOpenDialog() JRE 1.7
                      sabre150
                      Without access to your source I don't think I can help though I can offer a suggestion that you construct all your Swing dialogues up front and not on demand.