java 10 compilaton Null Pointer Exception

时间:2018-06-16 05:49:21

标签: java compiler-errors javac java-9 java-10

我最近安装了jdk10。我正在做正常的代码而且它无法正常工作。

我在这里做错了吗? 请参阅代码和异常堆栈跟踪。 据我所知,这种行为应该没有理由。

import com.bean.College;

public class Student {

    interface Club {
        <T> T get(College<T> key);
    }

    private Club club;

    Student() {
        Object obj = club.get(new College<>() {});
    }
}

导入的College类是:

public class College<T> {
    int id;
    protected College() {
    }

    College(int id){
        this.id=id;
    }
} 

在编译时,javac编译器与以下堆栈跟踪崩溃:

java.lang.NullPointerException
        at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitApply(Flow.java:1233)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1634)
        at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
        at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:396)
        at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitVarDef(Flow.java:987)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:956)
        at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
        at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:396)
        at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:57)
        at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitBlock(Flow.java:995)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1020)
        at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
        at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:396)
        at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitMethodDef(Flow.java:962)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:866)
        at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
        at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:396)
        at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitClassDef(Flow.java:925)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:774)
        at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
        at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:396)
        at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.analyzeTree(Flow.java:1325)
        at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.analyzeTree(Flow.java:1315)
        at jdk.compiler/com.sun.tools.javac.comp.Flow.analyzeTree(Flow.java:216)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1393)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1367)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:965)
        at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:306)
        at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:165)
        at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:57)
        at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:43) 

3 个答案:

答案 0 :(得分:15)

这是一个未解决的漏洞。该错误计划在jdk 11中解决。

JDK-8203195-匿名类类型推断导致NPE

Type:                 Bug
Status:               In Progress
Priority:             P2
Resolution:           Unresolved
Affects Version/s:    9, 10, 10.0.1, 11
Fix Version/s:        11
Component/s:          tools
Labels:               dcsfai reproducer-yes webbug 

Subcomponent:         javac
CPU:                  generic   
OS:                   generic

https://bugs.openjdk.java.net/projects/JDK/issues/JDK-8203195?filter=allopenissues

但是,在bug描述中提到了一个工作:

  

有趣的是,更改A.java以执行以下操作:
    Object baz => foo.foo(new B<Object>() {});
    或者将foo / B.java改为   以下内容:

 package foo;

 public class B<T> {

 B(int baz) {   }

 protected B() {   } 
 }
     

导致编译成功。

答案 1 :(得分:1)

只需替换'&lt;&gt;'使用显式类型参数。 这是修改过的Student类:

public class Student {
interface Club {
    <T> T get(College<T> key);
}

private Club club;

Student() {
    Object obj = club.get(new College<Object>() {});
}}

答案 2 :(得分:0)

即使使用我发给我的Java 11修复程序,错误报告的状态仍然是打开的