oracle / graal

GraalVM compiles Java applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀
https://www.graalvm.org
Other
20.26k stars 1.62k forks source link

Native images don't print "Helpful NullPointerExceptions" (JEP 358) #9680

Open zakkak opened 1 week ago

zakkak commented 1 week ago

Describe the issue JVM prints:

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

while the corresponding native image prints:

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

Steps to reproduce the issue

cat > HelloWorld.java <<EOF                                                                                                                                                                     
public class HelloWorld {

    static final String name = null;

    public static void main(String[] args) {
        System.out.println("Hello world! " + name.length());
    }

}
EOF
javac HelloWorld.java
java HelloWorld
native-image -cp ./ HelloWorld
./helloworld

Describe GraalVM and your environment:

More details Originally reported in https://github.com/quarkusio/quarkus/issues/43274

zakkak commented 3 days ago

FWIW in order to "fix" this, substratevm should implement

https://github.com/oracle/graal/blob/bd969aab3186e660842157e605020ff1b12b564b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaLangSubstitutions.java#L607-L611