1

次の単純なコードで Nullpointer 例外が発生します。

import javax.swing.JFileChooser;

public class Main {

    public static void main(String[] args) {
        JFileChooser chooser = new JFileChooser();
    }

}

スローするのは JFileChooser コンストラクターです。次の例外メッセージが表示されます。

Exception in thread "main" java.lang.NullPointerException
    at sun.awt.shell.Win32ShellFolder2.getFileSystemPath(Unknown Source)
    at sun.awt.shell.Win32ShellFolder2.access$400(Unknown Source)
    at sun.awt.shell.Win32ShellFolder2$10.call(Unknown Source)
    at sun.awt.shell.Win32ShellFolder2$10.call(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)

問題をグーグルで調べたときに見つけたのは、Java6 の回帰に関するレポートだけだったので、最初に行ったのは JDK 7u45 への更新で、古いバージョンのジャンクをすべて捨てました。現在、JRE 7u45 と JDK 7u45 のみがインストールされています。

折り返し電話があるので、最新情報を確認する必要がありますSystem.getProperty("java.runtime.version")1.7.0_45-b18

ただし、まだ同じエラーが発生します。私はEclipseでコードを実行していますが、標準のJRE以外のライブラリをリンクしたことはありません。

手がかりはありますか?

編集: JFileChooser コンストラクターにステップインしようとしたときのコール スタックです。

Thread [main] (Suspended)   
    FileNotFoundException(Throwable).<init>(String) line: 264   
    FileNotFoundException(Exception).<init>(String) line: not available 
    FileNotFoundException(IOException).<init>(String) line: not available   
    FileNotFoundException.<init>(String, String) line: not available    
    FileInputStream.open(String) line: not available [native method]    
    FileInputStream.<init>(File) line: not available    
    Toolkit$1.run() line: not available 
    AccessController.doPrivileged(PrivilegedAction<T>) line: not available [native method]  
    Toolkit.initAssistiveTechnologies() line: not available 
    Toolkit.<clinit>() line: not available
    Component.<clinit>() line: not available
    Main.main(String[]) line: 6 

編集 2 : のどの部分が問題を引き起こしているかを突き止めましたJFileChooser: コンストラクターの 2 番目のオプションのパラメーターは、FileSystemViewバグ アウトです。私が自分で書くと、うまくいきます:

static public class DummyFSV extends FileSystemView
    {

        public SingleRootFileSystemView(File root)
        {
            super();
        }

        @Override
        public File createNewFolder(File containingDir)
        {
            return null;
        }

        @Override
        public File getDefaultDirectory()
        {
            return null;
        }

        @Override
        public File getHomeDirectory()
        {
            return null;
        }

        @Override
        public File[] getRoots()
        {
            return null;
        }
    }
    ...
    JFileChooser = new JFileChooser( new DummyFVS() );

もちろん、私はそれで多くを行うことはできません。私が取得するデフォルトの FSVFileSystemView.getFileSystemView()WindowsFileSystemView(私の OS に適合する) ですが、使用されるとすぐに NPE になります。

ダミー FSV は実際にはファイル ダイアログを表示しますが、Windows FSV ではないため、フォルダーをナビゲートできません。そのため、非常に限られた用途にすぎません。

4

1 に答える 1