mscharhag, Programming and Stuff;

A blog about programming and software development topics, mostly focused on Java technologies including Java EE, Spring and Grails.

Sunday, 3 November, 2013

Groovy's magical NullObject

In this post I am going to explain some not that obvious differences of null in Java and null in Groovy.

Let's start with the following line:

Object o = null

This statement works fine in Java and Groovy (except that Java requires a semicolon at line end).
However, it has slightly different effects.

In Java null is a special literial, which is assigned to reference types that do not point to any object. Every time you try to do anything on a null reference (like calling methods or accessing member variables) a NullPointerException will be thrown.

In Groovy null is an object! It is an instance of org.codehaus.groovy.runtime.NullObject. Most times NullObject will throw a NullPointerException if you try to access a method or a member variable. However, there are a few methods that can be called on NullObject:

import org.codehaus.groovy.runtime.NullObject

assert NullObject == null.getClass()
assert       true == null.equals(null)
assert      false == null.asBoolean()
assert    "null!" == null + "!"
assert      false == null.iterator().hasNext()

As we can see the null object protects developers in some cases from NullPointerExceptions. asBoolean() returns always false and ensures that null can be converted to a boolean value when necessary. iterator() returns an instance of java.util.Collections$EmptyIterator. Because of that it is possible to safely iterate over objects without explicitly checking for null.

Interestingly I haven't found anything about NullObject in the official groovy documentation. It is not mentioned in Differences from Java nor in Groovy's Null Object Pattern.

There might be no practical use case for this but you can even create your own instance of NullObject:

Class c = null.getClass()
NullObject myNull = c.newInstance()

But be aware that the equals() method returns only true if you pass in the default NullObject instance. So it might not work correctly for your own NullObject instance:

assert false == myNull.equals(myNull)
assert  true == myNull.equals(null)

You can also modify the metaClass of NullObject to add you own methods:

NullObject.metaClass.myMethod = { println "I am null" }


  • Mike Miller - Saturday, 9 November, 2013

    Nicely done!

  • umlcat - Monday, 11 November, 2013


    Many people, including me, consider this as an Object Oriented Design Pattern. Thre is also the "Root Object" commonly found, in programming frameworks, that sometimes overlaps with the "Null Object", and sometimes, is used as a different class.

    Anyway, good work. Cheers.

Leave a reply