对 Paths.get() 的简单调用会抛出由 WindowsPathParser 中的 NullPointerException 引起的 ExceptionInInitializerError。我在 Windows7 Enterprise 上使用 Oracle jdk 1.8.0_131 64 位。
static Path outPath;
public static void main(String[] args) {
outPath = Paths.get("data");
}
异常堆栈跟踪
Exception in thread "main" java.lang.ExceptionInInitializerError
at java.nio.file.FileSystems.getDefault(FileSystems.java:176)
at java.nio.file.Paths.get(Paths.java:84)
at com.xpo.or.agg.specific.Program.instantiate(Program.java:62)
at com.xpo.or.agg.specific.Program.main(Program.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.NullPointerException
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:98)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at sun.nio.fs.WindowsFileSystem.<init>(WindowsFileSystem.java:57)
at sun.nio.fs.WindowsFileSystemProvider.<init>(WindowsFileSystemProvider.java:53)
at sun.nio.fs.DefaultFileSystemProvider.create(DefaultFileSystemProvider.java:36)
at java.nio.file.FileSystems$DefaultFileSystemHolder.getDefaultProvider(FileSystems.java:108)
at java.nio.file.FileSystems$DefaultFileSystemHolder.access$000(FileSystems.java:89)
at java.nio.file.FileSystems$DefaultFileSystemHolder$1.run(FileSystems.java:98)
at java.nio.file.FileSystems$DefaultFileSystemHolder$1.run(FileSystems.java:96)
at java.security.AccessController.doPrivileged(Native Method)
at java.nio.file.FileSystems$DefaultFileSystemHolder.defaultFileSystem(FileSystems.java:96)
at java.nio.file.FileSystems$DefaultFileSystemHolder.<clinit>(FileSystems.java:90)
我能想到的唯一问题是静态成员 Program 类初始化和 java.nio.file.FileSystems 初始化之间的一些副作用。
java.lang.ExceptionInInitializerError 表示静态初始化程序中发生了意外异常。 抛出 ExceptionInInitializerError 来表明 在评估静态初始值设定项或 静态变量的初始值设定项。
感谢任何帮助。
最佳答案
嗯...我认为问题在于 user.dir
系统属性由于某种原因为 null
。 (我猜)原因是你的 IDE 配置中的某个地方。例如:
public static void main(String[] args) {
System.getProperties().remove("user.dir");
outPath = Paths.get("data");
}
将在不同环境中重现您的确切问题:
Exception in thread "main" java.lang.ExceptionInInitializerError
at java.nio.file.FileSystems.getDefault(FileSystems.java:176)
at java.nio.file.Paths.get(Paths.java:84)
我是 Eclipse/STS 用户,此属性来自应用程序的运行配置,但我不确定您如何在 IntelliJ 中(错误)配置它。无论如何 - 缺少 user.dir
是你的问题。
关于Java8 FileSystems.getDefault() 抛出由 WindowsPathParser 中的 NullPointerException 引起的 ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44188542/