đž Archived View for hyperborea.org âş tech-tips âş groovy-null.gmi captured on 2023-09-28 at 15:56:36. Gemini links have been rewritten to link to archived content
âŹ ď¸ Previous capture (2022-04-28)
âĄď¸ Next capture (2024-08-31)
-=-=-=-=-=-=-
Groovy will let you call toString() on a null object. The result is the word ânullâ, which might be what youâre expecting if you know the object is null, but probably isnât what youâre expecting if you donât.
So if youâre, say, binding a SQL parameter and you forget to check for nulls like you would in Java, and you forget to use a null-safe operator like you should in Groovy, and you get a null value, what happens? Does groovyâŚ
Throw a NullPointerException like Java?
Set the field to null?
Neither. It sets the field to the string ânullâ.
Make sure to use myVariable?.toString() instead of myVariable.toString() for cases like this!
âKelson Vibber, 2018-03-28