United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4711700 : NullPointerException in JFileChooser when accessing system level icons

Details
Type:
Bug
Submit Date:
2002-07-08
Status:
Resolved
Updated Date:
2003-11-11
Project Name:
JDK
Resolved Date:
2003-09-26
Component:
client-libs
OS:
windows_xp,windows_2000,windows
Sub-Component:
javax.swing
CPU:
x86
Priority:
P2
Resolution:
Fixed
Affected Versions:
1.4.1,1.4.2,6u20
Fixed Versions:
1.4.2_04 (04)

Related Reports
Backport:
Duplicate:
Duplicate:
Relates:
Relates:

Sub Tasks

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
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
                                     
2003-08-18
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
                                     
2003-09-04
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;
    }
                                     
2004-06-11
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


                                     
2004-06-14



Hardware and Software, Engineered to Work Together