Why FileSystemView throw NPE in my scenario

主宰稳场 提交于 2019-12-11 15:48:22

问题


I'm using Java to get the map between volume label and drive letter and the code is like below:

import java.io.File;
import javax.swing.filechooser.FileSystemView;
public class Test{

    public void listMap(){
        File[] files = File.listRoots();
        System.out.println("The map between volume label and drive is: " );
        for(File file: files){
            String theMap = FileSystemView.getFileSystemView().getSystemDisplayName(file);
            System.out.println(theMap);
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.listMap();
    }
}

I have built above code into a exe file on windows through exe4j. It works fine when you run it directly. However, when I use Teamcity build agent to launch the exe, the exception below will be thrown when the FileSystemView.getFileSystemView().getSystemDisplayName(file) is called.

java.lang.NullPointerException
    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$Worker.runTask(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)

Any comment is appreciated.

来源:https://stackoverflow.com/questions/47616912/why-filesystemview-throw-npe-in-my-scenario

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!