java 14 nullpointerexception no detailed message

筅森魡賤 提交于 2020-07-20 07:10:38

问题


Java 14 has many new features. One of them is showing detailed message in NullPointerException. I installed Java 14 and trying to compile and run below class but I am not getting any detailed message. Am I missing anything? please help.

~/code/demo/temp$ java -version
openjdk version "14" 2020-03-17
OpenJDK Runtime Environment AdoptOpenJDK (build 14+36)
Eclipse OpenJ9 VM AdoptOpenJDK (build openj9-0.19.0, JRE 14 Mac OS X amd64-64-Bit Compressed          References 20200313_47 (JIT enabled, AOT enabled)
OpenJ9   - 0133ba037
OMR      - 1c04e0ef9
JCL      - a73be60649 based on jdk-14+36)

~/code/demo/temp$ cat Hello.java
public class Hello {
  public static void main(String args[]) {
    String a = null;
    System.out.println(a.length());
  }
}

~/code/demo/temp$ javac Hello.java
~/code/demo/temp$ java -XX:+ShowCodeDetailsInExceptionMessages Hello
Exception in thread "main" java.lang.NullPointerException
at Hello.main(Hello.java:4)

I am passing suggested -XX:+ShowCodeDetailsInExceptionMessages flag to java but there is no detailed message. Please help.


回答1:


OpenJ9 currently does not support JEP 358:

Extended messages for NullPointerException not yet implemented

JEP 358: Helpful NullPointerExceptions provides extended messages when a NullPointerException is generated by the Java 14 VM and you have enabled the feature. However, be aware that this is not implemented in OpenJ9 at this time.

The progress is tracked in this bug

If you want to use this feature, download the hotspot variant from adoptopenjdk. It's the same vendor as your current distribution, so this is only a small change.




回答2:


I am keeping this here for clarification, since the OP didn't say what the expected output is.

The following example works because it is using the hotspot jvm.

public class Exceptional{
    public static void main(String[] args){
        String a = null;
        try{
            System.out.println(a.length());
        } catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}

This is pretty much the same program, except it is just showing the message.

~/local/jdk-14+36/bin/java Exceptional.java 

null

When run with the additional argument.

 ~/local/jdk-14+36/bin/java -XX:+ShowCodeDetailsInExceptionMessages Exceptional.java

Cannot invoke "String.length()" because "" is null

The argument also affects the stacktrace on the hotspot jvm.

When run without the additional argument:

Exception in thread "main" java.lang.NullPointerException at Exceptional.main(Exceptional.java:6)

And run with the argument:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "" is null at Exceptional.main(Exceptional.java:6)

I incorrectly thought the stack trace might not change because it already includes additional information about the code. As the other answer points out, it is a bug that is being addressed in the openj9 jvm.



来源:https://stackoverflow.com/questions/61042145/java-14-nullpointerexception-no-detailed-message

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!