Java8 FileSystems.getDefault() 抛出由 WindowsPathParser 中的 NullPointerException 引起的 ExceptionInInitializerError

标签 java java-8 filesystems

对 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/

相关文章:

java - 将 Java 数组转换为 Clojure 列表或序列?

java - 使用 Base64 编码字符串的 XML 到 PDF

Java 8 optional 添加仅当 optional.isPresent 时才返回结果

php - 检查 200 万个文件是否有重复项

file-io - 如何用 MPI_File_open 替换 MPI 中的现有文件

powershell - 如何在不使用 attrib.exe 的情况下通过 Powershell 更改扩展的 Windows 文件属性?

java - Java 可以加载 Mac OSX 包/框架/或 dylib 吗?如何做呢?

java - 如何使用 ExecutorService 将两个方法作为线程一个接一个地运行

java - Redis 模型注释中数据类中的环境变量

java - 什么决定了从 lambda 创建哪个功能接口(interface)?