Groovy

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" }
null.myMethod()

 

Reference: Groovy’s magical NullObject from our JCG partner Michael Scharhag at the mscharhag, Programming and Stuff blog.

Michael Scharhag

Michael Scharhag is a Java Developer, Blogger and technology enthusiast. Particularly interested in Java related technologies including Java EE, Spring, Groovy and Grails.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button