I have the following code
<g:if test="${cart == null || cart.isEmpty()}">
Cart is Empty
</g:if>
<g:else>
${cart.size()} items
</g:else>
but the first time I access the site (when cart is null) I get a "Cannot invoke method isEmpty() on null object" exception
This can be rewritten as
<g:if test="${cart}">
${cart.size} items
</g:if>
<g:else>
Cart is Empty
</g:else>
If a variable is null or a Lists size is 0, it will be False according to the Groovy truth.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With