title: Null checking in spring expression language
body: <p>I am experiencing a strange behaviour when checking null pointer in a date field inside a collection object</p>
  
  <pre><code>public class FilterTest {
         public static void main(String[] args) {
                Collection&lt;EmployeeDto&gt; employees = new ArrayList&lt;EmployeeDto&gt;();
             employees.add(new EmployeeDto("", 1111, new Date()));
             employees.add(new EmployeeDto("123", 2222, null));
             employees.add(new EmployeeDto("22", 3333, new Date()));
             employees.add(new EmployeeDto("22", 4444, null));
             employees.add(new EmployeeDto("11", 5555, null));
             employees.add(new EmployeeDto("22", 6666, null));
             employees.add(new EmployeeDto(null, 6666,null));
  
             StandardEvaluationContext stdContext = new StandardEvaluationContext();
             stdContext.setVariable("emps", employees);      
  
             ExpressionParser parser = new SpelExpressionParser();    
  
  
            **String key4= "#emps.?[dateOfBirth != null ? T(DateUtil).formatDate(dateOfBirth), 'dd/MM/yyyy').contains('25/07/2012') : false]" ;**          
  
  
                Collection&lt;Object&gt; proj1 = (Collection&lt;Object&gt;) parser.parseExpression(
                        key4).getValue(stdContext);
                System.out.println(proj1);    
  
  
  
  }
  class EmployeeDto {
         String name;
         public String getName() {
                return name;
         }
  
         public void setName(String name) {
                this.name = name;
         }
  
         public int getAge() {
                return age;
         }
  
         public void setAge(int age) {
                this.age = age;
         }
  
         int age;
         Date dateOfBirth;
  
         public EmployeeDto(String name, int age, Date dateOfBirth) {
                this.name = name;
                this.age = age;
                this.dateOfBirth = dateOfBirth;
         }
  
      public Date getDateOfBirth() {
          return dateOfBirth;
      }
  
      public void setDateOfBirth(Date dateOfBirth) {
          this.dateOfBirth = dateOfBirth;
      }
  }
  </code></pre>
  
  <p>The expression should return collection of objects satisfying the condition. But now , I am getting an exception that dateOfBirth cannot be found on null. So I tried with a new expression </p>
  
  <pre><code> String key4= "#emps.?[dateOfBirth != null ? T(DateUtil).formatDate(#emps.![dateOfBirth], 'dd/MM/yyyy').contains('25/07/2012') : false]" ;
  </code></pre>
  
  <p>This works fine. But when I add null in the first object of the collection, it return results as 0, and when I checked, I found that null is passed to the DateUtil class formatDate method. ie the emploee collection is like 
      ` </p>
  
  <pre><code>employees.add(new EmployeeDto("", 1111, null)); // first value is null, and I am getting empty result, but there is one corrrect result
             **employees.add(new EmployeeDto("12", 2222, null));**
             employees.add(new EmployeeDto("22", 3333, new Date()));
             employees.add(new EmployeeDto("33", 4444, null));
             employees.add(new EmployeeDto("FIVE", 5555, null));
             employees.add(new EmployeeDto("SIX", 6666, null));
             employees.add(new EmployeeDto(null, 6666,null));
  </code></pre>
  
  <p>Could you please help me to solve this issue, can anyone tell the correct expression I should use?</p>