What does the question mark mean in GSP/Grails?

Amir Raminfar picture Amir Raminfar · Jan 3, 2011 · Viewed 16.4k times · Source

I saw this in my generated GSP pages. What does the ? mean?

<g:textField name="name" value="${phoneInstance?.name}" />

Answer

Burt Beckwith picture Burt Beckwith · Jan 3, 2011

It's the "Safe Navigation Operator", which is a Groovy feature that concisely avoids null pointer exceptions. See http://docs.groovy-lang.org/latest/html/documentation/index.html#_safe_navigation_operator

In this case, if phoneInstance is null, then it doesn't try to get the name property and cause a NPE - it just sets the value of the field tag to null.