我如何解决 Java 8 中的这个错误?

How can I work around this bug in Java 8?

我写了一段时间的应用程序,今天做了一些更改后,我在尝试 mvn clean install:

时遇到了这个错误
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 diag nostic 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.attribExpr(Attr.java:649)
        at com.sun.tools.javac.comp.Attr.visitVarDef(Attr.java:1093)
        at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852)
        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.DeferredAttr$DeferredAttrNode$StructuralStuckChecker.canLambdaBodyCompleteNormally(DeferredAttr.java:704)
        at com.sun.tools.javac.comp.DeferredAttr$DeferredAttrNode$StructuralStuckChecker.visitLambda(DeferredAttr.java:652)
        at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624)
        at com.sun.tools.javac.comp.DeferredAttr$DeferredAttrNode$StructuralStuckChecker.complete(DeferredAttr.java:605)
        at com.sun.tools.javac.comp.DeferredAttr$DeferredType.check(DeferredAttr.java:245)
        at com.sun.tools.javac.comp.DeferredAttr$DeferredType.access[=11=]0(DeferredAttr.java:132)
        at com.sun.tools.javac.comp.DeferredAttr$DeferredAttrNode.process(DeferredAttr.java:554)
        at com.sun.tools.javac.comp.DeferredAttr$DeferredAttrContext.complete(DeferredAttr.java:479)
        at com.sun.tools.javac.comp.Resolve.rawInstantiate(Resolve.java:578)
        at com.sun.tools.javac.comp.Resolve.selectBest(Resolve.java:1431)
        at com.sun.tools.javac.comp.Resolve.findMethodInScope(Resolve.java:1618)
        at com.sun.tools.javac.comp.Resolve.findMethod(Resolve.java:1689)
        at com.sun.tools.javac.comp.Resolve.findMethod(Resolve.java:1662)
        at com.sun.tools.javac.comp.Resolve.findConstructor(Resolve.java:2545)
        at com.sun.tools.javac.comp.Resolve.doLookup(Resolve.java:2514)
        at com.sun.tools.javac.comp.Resolve$BasicLookupHelper.lookup(Resolve.java:3074)
        at com.sun.tools.javac.comp.Resolve.lookupMethod(Resolve.java:3325)
        at com.sun.tools.javac.comp.Resolve.resolveConstructor(Resolve.java:2511)
        at com.sun.tools.javac.comp.Resolve.resolveConstructor(Resolve.java:2502)
        at com.sun.tools.javac.comp.Attr.visitNewClass(Attr.java:2097)
        at com.sun.tools.javac.tree.JCTree$JCNewClass.accept(JCTree.java:1516)
        at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
        at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:649)
        at com.sun.tools.javac.comp.Attr.visitVarDef(Attr.java:1093)
        at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852)
        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:169)
        at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:785)
        at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
        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:347)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:154)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:582)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
        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

我以前从未见过这样的事情。我该如何解决这个问题?

导致问题的线路:

int count = possibleCards.stream().reduce(0,
 (cnt, c) -> cnt += (c.getTags().contains(tag) ? 1 : 0),
 (cnt1, cnt2) -> cnt1 + cnt2);

这行代码应该计算所有具有特定标签的卡片对象。 该错误是可重现的。可以在此处找到更多详细信息:https://bugs.openjdk.java.net/browse/JDK-8056038?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab

Java8 的版本在某些平台上比 u25 早,而在其他平台上比 u31 早,通常会有古怪的问题,其中 lambda 和拆箱可能不能很好地结合在一起。解决方法可能是显式推断一些可能发生拆箱的 lambda 类型,例如:

(Integer count, c) -> count += c.getTags().contains(tag) ? 1 : 0)

但在这种特定情况下,您可以通过组合使用 mapToInt()sum() 来完全避免此问题,这样显然不会发生开箱恶作剧。

该问题本应在 u40 中修复,但截至撰写此答案时尚未发布。