Search code examples
grailsgroovy

Null check groovy way result of database query


This might seem simple but it's my first time dealing with groovy and database interactions

I have to get something from the database and check if it's NOT NULL, I'll throw an Exception

PaymentDetails details = PaymentDetails.findById(id)

What is the groovy way to check if the details is NOT NULL?

Is condition seems wrong :(

if (!details) {
    println("ERROR!!!!")
    throw new InvalidException()
}

Thanks in advance!


Solution

  • if (details) {
        println("ERROR!!!!")
        throw new InvalidException()
    }
    

    Refer to docs on The Groovy Truth to understand how Groovy decides on boolean expressions.