JDK-4711700 : NullPointerException in JFileChooser when accessing system level icons
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.1,1.4.2,6u20
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows,windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2002-07-08
  • Updated: 2003-11-11
  • Resolved: 2003-09-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.2_04 04Fixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description

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) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.4.2_04 tiger FIXED IN: 1.4.2_04 tiger INTEGRATED IN: 1.4.2_04 tiger tiger-b22
14-06-2004

SUGGESTED FIX From bug 4917110: The solution is very simple: [current implementation] private Image makeIcon(long l, boolean flag) { if (l > 0L) { ... //create icon } return null; } [should be] private Image makeIcon(long l, boolean flag) { if (l != 0L && l != -1L) { ... //create icon } return null; }
11-06-2004

WORK AROUND System.setProperty("swing.disableFileChooserSpeedFix", "true"); or java -Dswing.disableFileChooserSpeedFix=true Note that setting this property in 1.4.2 will undo the fix for bug 4712307. ###@###.### 2003-09-04
04-09-2003

EVALUATION This bug is reproducible in 1.4.1 fcs. Will re-test after integration of fix for bug 4745575. ###@###.### 2002-10-01 The Swing classes are not intended to be thread safe. You can only create and manipulate components on the Main thread until the AWT event dispatching thread (EDT) is started. After that, only make changes in your event listeners or by means of SwingUtilities.invokeLater(). Will close this as not a bug. ###@###.### 2003-08-18 Correction. This is not thread related. Bug in native image loading code. ###@###.### 2003-09-04
18-08-2003