编译中的一个奇怪的 NullPointerException Java 8(编译器(1.8.0_31))

A weird NullPointerException in compilation Java 8 (compiler (1.8.0_31))

我正在尝试在此环境中编译我的代码:

Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T19:29:23+02:00)
Maven home: /usr/local/sbin/apache-maven-3.2.5
Java version: 1.8.0_31, vendor: Oracle Corporation
Java home: /usr/local/sbin/jdk1.8.0_31/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.16.0-23-generic", arch: "amd64", family: "unix"

NullPointerException 是从 com.sun.tools.javac.code.Types.isConvertible()

抛出的

我看到这个异常被报告为一个错误,但我找不到应该做什么来避免它(也许使用其他版本的 java 或 maven?)

有人知道吗?

这是堆栈跟踪:

[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ tnet ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1358 source files to /tradair/sources/java/target/classes
An exception has occurred in the compiler (1.8.0_31). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
java.lang.NullPointerException
    at com.sun.tools.javac.code.Types.isConvertible(Types.java:290)
    at com.sun.tools.javac.comp.Check.assertConvertible(Check.java:922)
    at com.sun.tools.javac.comp.Check.checkMethod(Check.java:876)
    at com.sun.tools.javac.comp.Attr.checkMethod(Attr.java:3838)
    at com.sun.tools.javac.comp.Attr.checkIdInternal(Attr.java:3615)
    at com.sun.tools.javac.comp.Attr.checkMethodIdInternal(Attr.java:3522)
    at com.sun.tools.javac.comp.Attr.checkMethodId(Attr.java:3501)
    at com.sun.tools.javac.comp.Attr.checkId(Attr.java:3488)
    at com.sun.tools.javac.comp.Attr.visitSelect(Attr.java:3370)
    at com.sun.tools.javac.tree.JCTree$JCFieldAccess.accept(JCTree.java:1897)
    at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
    at com.sun.tools.javac.comp.Attr.visitApply(Attr.java:1843)
    at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1465)
    at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
    at com.sun.tools.javac.comp.Attr.visitReturn(Attr.java:1704)
    at com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1384)
    at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
    at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
    at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:692)
    at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1142)
    at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
    at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
    at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
    at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:1035)
    at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
    at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
    at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
    at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4342)
    at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4252)
    at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4181)
    at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4156)
    at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1248)
    at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:901)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:860)
    at com.sun.tools.javac.main.Main.compile(Main.java:523)
    at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
    at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
    at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess(JavaxToolsCompiler.java:126)
    at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile(JavacCompiler.java:170)
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:684)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:118)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
    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:483)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] An unknown compilation problem occurred
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.622 s
[INFO] Finished at: 2015-01-29T10:51:50+02:00
[INFO] Final Memory: 34M/509M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile (default-compile) on project tnet: Compilation failure
[ERROR] An unknown compilation problem occurred
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

这是旧版本 OpenJDK 中的 known bug。您应该尝试至少更新到 8u40,因为它显然已在那里解决。

也可能是this bug,标记为未解决。 (但这可能与 Quirliom 在 OpenJDK 跟踪器中发现的错误相同......而且 "Bugs Database" 人们没有注意到存在重复。)

正如 Quirliom 所说,这是编译器中的一个严重错误。如果你能找到有问题的代码,你可以通过将 lambda 赋值给一个变量来解决它。 FunctionalJava 有几次 运行(例如 https://github.com/functionaljava/functionaljava/issues/76)。我们解决了这样的问题代码:

return foldLeft((i, a) -> i + 1, 0);

通过分配给一个变量:

F2<Integer, A, Integer> f = (i, a) -> i + 1; return foldLeft(f, 0);