java 14 nullpointerexception no detailed message

2.4k Views Asked by At

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.

2

There are 2 best solutions below

8
On

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.

3
On

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.