生产环境和本地环境中,NullPointerException的日志记录不一致

编程语言 2026-07-08

In prod a NPE is logged like

java.lang.NullPointerException: 无法读取数组长度,因为 "" 为null

while on my local machine it is logged like

java.lang.NullPointerException: 无法读取数组长度,因为 "" 为null

The class, which throws the NPE is from Java itself (MessageDigest). I have read about https://openjdk.org/jeps/358. In my understanding, this should be a matter of how the class is compiled. Therefore I ensured, that both environments use the same Java (Ecplise Temurin 17.0.15+6).

有何想法,这个差异来自何处?我想弄清楚原因——最好能让我掌握把本地环境配置成生产环境镜像的知识。

解决方案

Check that you are really using the same Java version in local and prod. Consider this test program:

public class Main {
    public static void main(String... args) throws NoSuchAlgorithmException {
        MessageDigest.getInstance("SHA-256").update((byte[])null);
    }
}

If I launch with Java from a JDK it prints MessageDigest issue with named parameter "input", and MessageDigest.java file + line information:

Exception in thread "main" java.lang.NullPointerException: 
   Cannot read the array length because "input" is null
        at java.base/java.security.MessageDigest.update(MessageDigest.java:407)
        at stackoverflow.Main.main(Unknown Source)

If I launch with JRE produced by jlink --strip-debug it prints MessageDigest issue without specific named parameter, adding "<parameter1>" instead, and (Unknown Source) in place of file + line number. This is because the debug symbols are missing from the JDK code:

Exception in thread "main" java.lang.NullPointerException:
  Cannot read the array length because "<parameter1>" is null
    at java.base/java.security.MessageDigest.update(Unknown Source)
    at stackoverflow.Main.main(Unknown Source)

If this issue occurs only in your own libraries, check the value of javac flags -g or -Djavac.debug with -Djavac.debuglevel.

If building with javac with -g or its equivalent -Djavac.debug=true -Djavac.debuglevel=lines,vars,source then the exceptions originating in your code would output extended information, whereas when compiled with -g:none or equivalent -Djavac.debug=false the less specific style of message is used.

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章