我正在尝试在 Windows 7 下运行一些 javafx 应用程序。它在 archlinux 下运行良好。
我按以下方式打开文件选择器对话框:
@Override
public void start(final Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialDirectory(myInitialDir);
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Wav files (*.wav)", "*.wav");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showOpenDialog(primaryStage);
}
我上面提到的 - 在 linux 下一切正常,但在 windows 下我得到奇怪的 NPE:
Error:80070057 in SHCreateItemFromParsingName((PCWSTR)folder, NULL, IID_IShellItem, (void **)&pItem)
COM Error:80070057 0@0.
Error:80004005 in pOpenDialog->GetResults(&pFiles)
COM Error:80004005 5>?>7=0==0O >H81:0
java.lang.NullPointerException
at com.sun.glass.ui.CommonDialogs.showFileChooser(CommonDialogs.java:120)
at com.sun.javafx.tk.quantum.QuantumToolkit.showFileChooser(QuantumToolkit.java:1486)
at javafx.stage.FileChooser.showDialog(FileChooser.java:285)
at javafx.stage.FileChooser.showOpenDialog(FileChooser.java:234)
at ru.gkalabin.diploma.gui.equalizer.EqualizerDemo$1.handle(EqualizerDemo.java:107)
有什么办法可以解决这个问题吗?
最佳答案
这是个问题:
RT-21999 Win: FileChooser does not accept valid initial directory
问题影响版本:2.2;修复版本:Lombard,因此修复在 JFX8(在 JDK8 中)中可用。
要解决此问题,请调用 <fileName>.getCanonicalPath()
用于初始文件夹。
来自开发者的评论:
This issue is Windows 7-specific. It's not reproducible on older versions of Windows.
A workaround is to pass an absolute path as an initial directory for the file chooser (see File.getAbsolutePath()/getCanonicalPath()).
来自用户的评论:
Jason Winnebeck added a comment - Mar, 18 2013 04:30 PM I've encountered this as well, with the exact same COM errors printed to stderr, resuling in NullPointerException. I actually tried getAbsolutePath at first, but that doesn't work for all paths, for some you have to use getCanonicalPath, thankfully I found this ticket that gave me that workaround. So, I don't have much additional information to offer, except here is some groovy code that reproduces it:
public void openFile() throws IOException {
FileChooser chooser = new FileChooser()
chooser.title = "Select FXML"
chooser.setInitialDirectory( new File( "." ) );
chooser.extensionFilters.add( new FileChooser.ExtensionFilter(
"FXML or SVG Files",
["*.fxml", "*.svg"]
))
Window window = sceneProperty.get().window
File selected = chooser.showOpenDialog( window )
}
无论其他设置如何(扩展过滤器、使用哪个窗口或为空)都会发生错误。使用 new File( "." ).canonicalFile
解决了这个问题,所以根据其他报告,似乎有 .
和 ..
在路径中是可能的原因。
关于java - 文件选择器中的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16822166/