Friday Repost: Groovy and Null

The Friday Repost series are copies from my earlier writings. Since I don’t want to loose them, and they might prove useful to others, I’m reposting them on this blog

To start, I find it surprising that there’s so little information to be found about this topic. Maybe it’s just me, so let’s start with a test. If you pass all questions, you don’t win much, but then there’s no need to further read this blog I guess.

So, without further ado, question 1.

Question 1

What’s the result of the following code?

Please remember your answer. Now, question number 2.

Question 2

What’s printed in the following code?

Same here: please remember your answer! Next, question number 3. Almost there!

Question 3

What’s the result of the following code?

Same here: please remember your answer! Next, last question!

Question 4

What’s the result of the following code (tip: check question 1!)?

Got every answer written down? Okay, here come the answers!


Answer 1: “null”
Answer 2: nothing!
Answer 3: A NullPointerException. (java.lang.NullPointerException: Cannot get property ‘name’ on null object)
Answer 4: Another NullPointerException.

All answers correct? Great, you can stop reading now, this blog doesn’t teach you anything. Some errors? Well, maybe there’s a chance to learn something, cause I’ll explain why this happens.

Null object

Groovy has the concept of “Null Objects”. If you do a “null.getClass()”, you will see that the class of “null” is a NullObject, or more precise, org.codehaus.groovy.runtime.NullObject. This class is a used whenever you declare a variable with null, as in “def name = null”. Having a NullObject instead of a Java null reference prevents a lot of NullPointerExceptions for most common operations. However, there are some exceptions!

Answer 1
Because the name is set to null, it’s actually set to NullObject. The toString of NullObject returns “null”, which makes sense, so you’ll see the String “null” printed whenever you call the toString(). It might make sense, but coming from a Java background and not knowing about the NullObject, I was kinda surprised to see it work!

Answer 2
Besides providing some good default implementations for some standard methods, the NullObject also has an iterator() method which provides an iterator over an empty list (Collections.EMPTY_LIST). This allows methods like each, but also collect, find, findAll, inject, every, any, etc (no, this is not a method, it just means etcetera) to safely work on any null collection, which is really nice!

Answer 3
So, while the null object works for most standard methods, there is a limit to the tolerance of it. Any properties or methods not in the NullObject class will still throw an exception when the object is null object. This is explicitly done in the NullObject by throwing an exception in the ‘setProperty’ and ‘invokeMethod’ methods. Just so you know.

Answer 4
Well, already the last anwer in this post. I thought, that when a toString() works, an equals works, a hashCode certainly must also work. And I expected it to return a zero (0). But it doesn’t. It throws an exception as explicitly programmed in the code. To be honest, I didn’t expect this, but I thought it would make sense when using it in a Hashtable, for example. It doesn’t, because a Hashtable already checks if the value is null, I thought this done by the by the ‘is’ method in NullObject (which returns true when comparing the object to “null”), but I have been corrected by Jochen Theodorou: since Hashtable is Java, it has no knowledge about the Groovy ‘is’ method. Also, I haven’t found out why the null object doesn’t return a 0, as I expected, but rather explicitly throws a NullPointer, but at least it’s something to be aware of!


Well, I hope reading this far helped a little in understanding Groovy and nulls. Using Groovy could give you some surprises, especially when you have a Java background, so I hope that after reading this, you’ll have a little less unexpected surprises!

3 replies
  1. fumokmm
    fumokmm says:


    Thank you for marvelous article. This is very interesting.
    I’m considering translate to Japanese and introduce in my blog.
    Would you give me a permission it?

    Thank you.

Trackbacks & Pingbacks

Comments are closed.