2

So many times, we do a null check of a field value and then compare them

boolean checkAmountGreaterThan10k(Opportunity record){
    if(record.Amount!=null){
       return record.Amount > 10000;
    }
    return false;
}

Then I recently discovered that any comparison operation performed on null value returns false.

Something like :

Integer bla = null;
System.debug(bla > 10000);//Prints 'false'
System.debug(bla >= 10000);//Prints 'false'
System.debug(bla < 10000);//Prints 'false'
System.debug(bla <= 10000);//Prints 'false'

System.debug( bla + 10000); // throws Null Ptr Exception

Thus I was thinking to use this in the above checkAmountGreaterThan10k as

boolean checkAmountGreaterThan10k(Opportunity record){  
       return record.Amount > 10000;    
}

Is this a documented behavior of comparison operator involving null, I was thinking about saving many lines of code with this.

I came from java world where I used to check null every time, similar java equivalent code throws NullPtrException

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println("Hello World");
        Integer i = null ;
        System.out.println(i < 20); //Exception in thread "main" java.lang.NullPointerException,
    }
}

Java code link:

Is the Apex Runtime not throwing a null ptr exception in Apex a bug or undocumented feature?

  • 1
    Numeric comparison against false used to throw NPE, I am 100% certain. Interesting that they would make this change and not announce it. – Adrian Larson Oct 24 '19 at 18:48
1

I find this behavior documented in the Apex Developer Guide's Expression Operators for the comparison operators:

  • Less than operator
  • Greater than operator
  • Less than or equal to operator
  • Greater than or equal to operator.

If x or y equal null and are Integers, Doubles, Dates, or Datetimes, the expression is false.

  • 1
    Ah nice... awesome that I can use it without worrying – Pranay Jaiswal Oct 24 '19 at 18:59
1

Surprisingly (to me), this behavior is documented. I have pasted the reference for <, but the same statement is made for <=, >, and >=.

Operator <

Syntax
x < y

Description
Less than operator. If x is less than y, the expression evaluates to true. Otherwise, the expression evaluates to false.
Note:

  • Unlike other database stored procedures, Apex does not support tri-state Boolean logic, and the comparison of any two values can never result in null.
  • If x or y equal null and are Integers, Doubles, Dates, or Datetimes, the expression is false.
  • A non-null String or ID value is always greater than a null value.
  • If x and y are IDs, they must reference the same type of object. Otherwise, a runtime error results.
  • If x or y is an ID and the other value is a String, the String value is validated and treated as an ID.
  • x and y cannot be Booleans.
  • The comparison of two strings is performed according to the locale of the context user and is case-insensitive.

On the contrary, the following operators note

x and y cannot be null

  • +=
  • *=
  • -=
  • /=
  • |=
  • &=
  • &&
  • ||

Surprisingly, this indication is not made for arithmetic operations, even though it still holds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.