Michael Scharhag

About 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" }


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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply

− four = 1

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below: