Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 637
Heathen Avatar asked May 27 '11 09:05

Heathen


1 Answers

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

like image 125
Tomasz Błachowicz Avatar answered Oct 05 '22 23:10

Tomasz Błachowicz