NEW COURSE 👨🏼‍🏫 Up and Running with Vue.js is available! 🎉 Get it now!

Groovy's null safe operator

August 20, 2013 • ☕️ 1 min read

The more years I get under my belt as a programmer the more I appreciate the smaller things a language has to offer. Today I want to look at Groovy's null safe operator. When you want to crawl an object graph you always have to be worried about the dreaded null pointer exception. First I want to take a look at this example in ColdFusion. In this example we are looking at the body of a method where we load up a user object. We want to grab the city of the user object but in each step of the object graph we need to make sure that call is not null before preceding.

var city = "";
var user = entityLoadByPK("User",10);

if ( !isNull(user) ){
	if( !isNull( user.getAddress() ) ) {
		if( !isNull(user.getAddress().getCity() ) ) {
			city = user.getAddress().getCity();
		}
	}
}

return city;

In Groovy we can do this shorthand by using the null safe operator (?.). If the variable before the question mark is null it will not proceed and actually returns null for you. We could even shorten this statement more but you get the idea.

def user = User.get(10)

return user?.address?.city

The Groovy language has so many little things like this that I am really starting to love. What are some of your favorite features in the language?

Follow me on Twitter, LinkedIn, or sign up for my newsletter.

Dan Vega

I’m a Husband, Father, Curriculum Developer and maker of things from Cleveland Ohio. I created this website as a place to document my journey as I learn new things and share them with you. I have a real passion for teaching and I hope that one of blog posts, videos or courses helps you solve a problem or learn something new.