Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4711700

NullPointerException in JFileChooser when accessing system level icons

XMLWordPrintable

    • 04
    • x86
    • windows, windows_2000, windows_xp



        Name: jk109818 Date: 07/08/2002


        FULL PRODUCT VERSION :
        java version "1.4.1-beta"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b14)
        Java HotSpot(TM) Client VM (build 1.4.1-beta-b14, mixed mode)

        Microsoft Windows 2000 [Version 5.00.2195]






        A DESCRIPTION OF THE PROBLEM :
        NullPointerException thrown by JFileChooser, but also
        currupts any future access to the fileChooser and
        FileSystemView

        There seems to be a resource locking problem when accessing
        system level icons

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the following code



        EXPECTED VERSUS ACTUAL BEHAVIOR :
        The program should effectivaly run for ever, however within
        ten minutes it crashes. This is a more extream test case.

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(ImageIcon.java:161)
        at javax.swing.ImageIcon.<init>(ImageIcon.java:147)
        at
        com.sun.java.swing.plaf.windows.WindowsFileChooserUI$ShortCutPanel.<init>
        (WindowsFileChooserUI.java:592)
        at
        com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installComponents
        (WindowsFileChooserUI.java:355)
        at javax.swing.plaf.basic.BasicFileChooserUI.installUI
        (BasicFileChooserUI.java:130)
        at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installUI
        (WindowsFileChooserUI.java:175)
        at javax.swing.JComponent.setUI(JComponent.java:449)
        at javax.swing.JFileChooser.updateUI(JFileChooser.java:1700)
        at javax.swing.JFileChooser.setup(JFileChooser.java:345)
        at javax.swing.JFileChooser.<init>(JFileChooser.java:320)
        at javax.swing.JFileChooser.<init>(JFileChooser.java:273)
        at Test$MyFileChooser.<init>(Test.java:117)
        at Test.getFileChooser(Test.java:113)
        at Test$2.run(Test.java:76)

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        /*
         * Test.java
         *
         * (C) Yolus Limited
         * All rights reserved
         *
         */
        import java.io.*;
        import javax.swing.*;
        import java.awt.event.*;
        import java.awt.*;
        import javax.swing.filechooser.*;

        /**
         *
         */
        public class Test {
            static int count;
            private static FileSystemView fileSystemView =
        FileSystemView.getFileSystemView();

            public static void scanDirectory(File parent) {
                File[] files = parent.listFiles();
                if (files == null)
                    return;
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        scanDirectory(files[i]);
                    } else {
                        try {

                            Object icon = fileSystemView.getSystemIcon(files[i]);
                            if (icon==null) {
                                System.out.println("Failed to get icon for file " +
        files[i]);
                                System.exit(-1);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            System.exit(-1);
                        }
                    }
                    if (++count >= 10) {
                        count = 0;
                        // gc and sleep just to prove finalizer doesn't help
                        System.gc();
                    }
                }
            }


            public static void main(String[] arg) throws Exception {
                UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

                final JFrame temp = new JFrame("Look!");
                temp.pack();
                temp.show();
                temp.dispose();
                for (int q=0;q<10;q++) {
                    SwingUtilities.invokeLater( new Runnable() {
                        public void run() {
                            try {
                                final File[] roots = fileSystemView.getRoots();
                                for (int i = 0; i < roots.length; i++) {
                                    Thread thread = new RootThread(roots[i]);
                                    thread.start();
                                }
                            } catch (Throwable t) {
                                t.printStackTrace();
                                System.exit(-1);
                            }
                        }
                    });
                }
                JFrame frame = new JFrame( "Panel");
                Thread fileChooserThread = new Thread() {
                    public void run() {
                        final JDialog fileChooser = getFileChooser(temp);
                        synchronized (this) {
                            try {
                                Thread.currentThread().sleep(100*60);
                                fileChooser.dispose();
                            } catch (Throwable ignore) {
                            }
                        }
                    }
                };
                synchronized (frame) {
                    try {
                        Thread.currentThread().sleep(10 * 60 * 100);
                        fileChooserThread.start();
                    } catch (Throwable ignore) {
                    }
                }

                frame.addWindowListener( new WindowAdapter() {
                    public void windowClosing( WindowEvent e ) {
                        System.exit(0);
                    }
                });
                frame.pack();
                frame.show();
            }

            static class RootThread extends Thread {
                private File file;
                public RootThread(File file) {
                    this.file = file;
                }
                public void run() {
                    scanDirectory(file);
                }
            }
            public static JDialog getFileChooser(final JFrame temp) {
                MyFileChooser fileChooser = new MyFileChooser();
                 return fileChooser.createDialog(temp);
            }

            static class MyFileChooser extends JFileChooser {
                public JDialog createDialog(Component parent) {
                    return super.createDialog(parent);
                }
            }
        }

        ---------- END SOURCE ----------
        (Review ID: 159025)
        ======================================================================

              leifs Leif Samuelsson (Inactive)
              jkimsunw Jeffrey Kim (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: