Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8059443

Logical NOT operator throws NullPointerException for null Boolean return values

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 8u20
    • core-libs
    • b39
    • x86_64
    • windows_7

        FULL PRODUCT VERSION :
        java version "1.8.0_20"
        Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
        Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)


        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [Version 6.1.7601]

        A DESCRIPTION OF THE PROBLEM :
        Using the Nashorn Javascript engine, a statement that uses the logical NOT operator to negate the value return by a method defined with a boxed Boolean return type results in a NullPointerException if the returned value is null. In Javascript null is considered a falsey value, so the statement should result in a 'true' value.

        !null // returns true

        !myJavaObject.getNullBooleanValue() // should return true if return value is defined as Boolean and returns null

        If the referenced method is defined to return Object rather than Boolean, the statement returns the expected "true" value. When defined as Boolean return type a NullPointerException is thrown.



        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Execute the provided test case


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        !null = true
        !checkKittens = true
        !profile.getNullValue() = true
        !profile.isLikesKittens() = true
        ACTUAL -
        !null = true
        !checkKittens = true
        !profile.getNullValue() = true
        Exception in thread "main" java.lang.NullPointerException
        at sun.invoke.util.ValueConversions.unboxBoolean(Unknown Source)
        at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:535)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:568)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:525)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:521)
        at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:192)
        at javax.script.AbstractScriptEngine.eval(Unknown Source)
        at NashornBooleanTest.eval(NashornBooleanTest.java:22)
        at NashornBooleanTest.main(NashornBooleanTest.java:18)


        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        Exception in thread "main" java.lang.NullPointerException
        at sun.invoke.util.ValueConversions.unboxBoolean(Unknown Source)
        at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:535)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:568)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:525)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:521)
        at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:192)
        at javax.script.AbstractScriptEngine.eval(Unknown Source)
        at NashornBooleanTest.eval(NashornBooleanTest.java:22)
        at NashornBooleanTest.main(NashornBooleanTest.java:18)

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import javax.script.Bindings;
        import javax.script.ScriptEngine;
        import javax.script.ScriptEngineManager;
        import javax.script.ScriptException;
        import javax.script.SimpleBindings;


        public class NashornBooleanTest {

        public static void main(String[] args) throws ScriptException {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
        Bindings bindings = new SimpleBindings();
        bindings.put("checkKittens", null);
        bindings.put("profile", new Profile());
        eval("!null", engine, bindings);
        eval("!checkKittens", engine, bindings);
        eval("!profile.getNullValue()", engine, bindings);
        eval("!profile.isLikesKittens()", engine, bindings);
        }

        private static void eval(String statement, ScriptEngine engine, Bindings bindings) throws ScriptException {
        System.out.println(statement + " = " + engine.eval(statement, bindings));
        }

        public static class Profile {
        Boolean likesKittens;

        public Boolean isLikesKittens() {
        return likesKittens;
        }

        public Object getNullValue() {
        return null;
        }
        }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Write statement without using logical not operator (!) or change method return type to Object.

          1. NashornBooleanTest.java
            1 kB
            Sundararajan Athijegannathan

              attila Attila Szegedi
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: