Search code examples
springspring-el

Can Spring EL (SpEL) be configured to ignore null objects in the middle of an expression


If I have the expression: obj1.obj2.obj3

And obj2 is null, then expression fails with an exception. Is there any way to configure SpEL to just return null?


Solution

  • You should use safe navigation operator that is ?. (in your example that'd be obj1?.obj2?.obj3) to avoid nasty NullPointerException while navigating graph of beans.

    You can find detailed explanation and some examples in chapter 6.5.15 Safe Navigation operator of reference