Java 14空指针异常,没有详细消息。

huangapple go评论76阅读模式
英文:

java 14 nullpointerexception no detailed message

问题

Java 14有许多新功能之一是在NullPointerException中显示详细信息。我安装了Java 14并尝试编译和运行下面的类,但是我没有得到任何详细的消息。我有什么遗漏吗?请帮忙。

~/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)

我正在将建议的-XX:+ShowCodeDetailsInExceptionMessages标志传递给java,但没有详细的消息。请帮忙。

英文:

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

得分: 10

OpenJ9目前不支持JEP 358

目前尚未实现针对NullPointerException的扩展信息

JEP 358: Helpful NullPointerExceptions为Java 14虚拟机生成的NullPointerException提供了扩展信息,如果你已启用此功能。然而,请注意,OpenJ9目前尚未实现此功能。

该进展在此错误中进行跟踪。

如果您想使用此功能,请从adoptopenjdk下载热点变体。这与您当前的发行版本是同一供应商,因此只需要做出小的更改。

英文:

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

得分: -1

我在这里保留这些内容以供澄清,因为原帖中没有说明预期输出是什么。

以下示例有效,因为它使用了热点虚拟机(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());
        }
    }
}

这基本上是相同的程序,只是显示了消息。

运行以下命令:

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

输出为:

null

当附加参数运行时:

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

输出为:

Cannot invoke "String.length()" because "<local1>" is null

该参数还会影响热点虚拟机上的堆栈跟踪。

在没有附加参数的情况下运行:

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

在使用参数运行时:

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

我错误地认为堆栈跟踪可能不会改变,因为它已经包含了关于代码的附加信息。正如其他答案指出的那样,这是一个正在openj9虚拟机中得到解决的错误。

英文:

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 "<local1>" 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 "<local1>" 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.

huangapple
  • 本文由 发表于 2020年4月5日 19:52:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61042145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定