12

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Pradeep
  • 419
  • 5
  • 14

2 Answers2

10

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.

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
-1

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.

matt
  • 10,892
  • 3
  • 22
  • 34
  • 1
    It does not answer WHY op does not get any helpful nullpointerexception. Just a "Hey, you can get the message in your code with `e.getMessage()`". – Johannes Kuhn Apr 05 '20 at 12:14
  • @JohannesKuhn doesn't it though? They're letting the exception bubble up to a stack trace where as the argument is modifying the exceptions message. So I am showing that the message is getting changed. – matt Apr 05 '20 at 12:16
  • 1
    The problem lies somewhere else. Even if op would run your code, it does not change the outcome (always `null` as message) – Johannes Kuhn Apr 05 '20 at 12:18
  • 1
    The message from `e.getMessage()` should be the same one shown in the stack trace if you don't catch the exception - unless you've tested this and observed a difference in the message between catching it vs. not catching it, then I don't think catching it addresses the issue in the question. – kaya3 Apr 05 '20 at 12:24
  • @kaya3 true, I updated it to include the behavior of the stack trace version too. – matt Apr 05 '20 at 12:29
  • The OP stated that they add the command line option but it still doesn't work. So your answer is not applicable to their problem. – RealSkeptic Apr 05 '20 at 13:34
  • @RealSkeptic It doesn't solve their issue, but I think it is relevant because it shows the expected output of using their argument in an environment where it works. – matt Apr 05 '20 at 13:39
  • @matt The down vote is presumably because you didn't answer the question. Even you acknowledge *"it doesn't solve their issue"*! – skomisa Apr 09 '20 at 06:54