Home » JVM Languages » Groovy » Groovy’s magical NullObject

About Michael Scharhag

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.

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*


seven − = 4

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.