Search code examples
jsongrailsgroovyoperatorsgsp

Will Grails GSP Safe Navigation Operator protect against "JSONObject$Null"?


In my Grails GSP I have the below code...

<div class="starshipStatus">${ship.engine.report?.substring(0,40)}... </div> 

where the 'report' member can sometimes have an empty value. I am sometimes getting this exception on that page...

 org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: 
 Error executing tag <g:render>: 
 No such property: title for class: 
 org.codehaus.groovy.grails.web.json.JSONObject$Null

Could that exception be originating from that line or does the Safe Navigation Operator ('?') protect against "JSONObject$Null"?


Solution

  • The null safe operator ? in Groovy will not help avoid this error because NULL is not the same as JSONObject.NULL. The latter is an actual object that represents the value of NULL and isn't actually NULL.

    You can however do something like this:

    <g:if test="${!ship.engine.report.equals(null)}">
    ...
    </g:if>