help-jel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

String null comparison failure


From: Mark Taylor
Subject: String null comparison failure
Date: Tue, 4 Jan 2022 11:23:54 +0000 (GMT)

Dear Konstantin,

happy new year!  And a JEL question.

If I evaluate a string equality test in which one of the operands is null,
a NullPointerException results, e.g.:

   x=="abc"

provokes a NullPointerException if x has the value null.
Since the equivalent test in Java simply returns false, that's not
what I was expecting.  Is this intended behaviour?

The following program illustrates this:

    import gnu.jel.CompiledExpression;
    import gnu.jel.Evaluator;
    import gnu.jel.Library;

    public class Cmp {
        public static void main(String[] args) throws Throwable {
            Library lib = new Library(new Class[] {Cmp.ALib.class}, null, null, 
null, null);
            compare("textValue", lib);
            compare("nullValue", lib);
        }
        private static void compare(String func, Library lib) throws Throwable {
            String txtExpr = func;
            String cmpExpr = func + "==\"abc\"";

            CompiledExpression txtCompex = Evaluator.compile(txtExpr, lib);
            CompiledExpression cmpCompex = Evaluator.compile(cmpExpr, lib);

            System.out.println();
            report(txtExpr, txtCompex);
            report(cmpExpr, cmpCompex);
        }
        private static void report(String expr, CompiledExpression compex) 
throws Throwable {
            System.out.println(expr);
            Object result = compex.evaluate(null);
            System.out.println("    -> " + represent(result));
        }
        private static String represent(Object obj) {
            if (obj == null) {
                return "null";
            }
            else if (obj instanceof String) {
                return "\"" + obj + "\"";
            }
            else {
                return obj.toString();
            }
        }
        public static class ALib {
            public static String textValue() {
                return "text";
            }
            public static String nullValue() {
                return null;
            }
        }
    }

which produces the output (JEL 2.1.2):

    textValue
        -> "text"
    textValue=="abc"
        -> false

    nullValue
        -> null
    nullValue=="abc"
    Exception in thread "main" java.lang.NullPointerException
            at java.text.RuleBasedCollator.compare(RuleBasedCollator.java:357)
            at gnu.jel.CompiledExpression.compare(CompiledExpression.java:352)
            at dump.evaluate_boolean(Unknown Source)
            at gnu.jel.CompiledExpression.evaluate(CompiledExpression.java:157)
            at Cmp.report(Cmp.java:25)
            at Cmp.compare(Cmp.java:21)
            at Cmp.main(Cmp.java:10)

Thanks!

Mark

--
Mark Taylor  Astronomical Programmer  Physics, Bristol University, UK
m.b.taylor@bristol.ac.uk          http://www.star.bristol.ac.uk/~mbt/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]