Thomas Sundberg

July 28, 2011

Do not return null

This post has been migrated to

Using your IDE to automatically create methods for you maybe a disaster waiting to happen.

When test driving your code, you always start with a test. The test will then demand that some functionality is implemented. Modern IDEs are a great support in this. All you have to do is to mark the class or method in your test that isn’t implemented and have your IDE implement it for you. An IDE cannot know what you should implement and will therefore create a rude, but compiling suggestion. This often means an implementation like this:

public Car create(Car car) {
    return null;

This is fine if you execute this code the first thing you do. You will then implement it properly and make sure that the test passes. But what happens if you mock the behaviour and make sure that the interactions works? Then chances are that you forget about the return null above. A NullPointerException is waiting to be thrown.

I have started to use another approach, throw a sun.reflect.generics.reflectiveObjects.NotImplementedException instead. It is harmless and you cannot get past it when executing your code. It signals that this is an area where the developer were planning to return and fix, but forgot.

An example could be:

public Car find(String registrationNumber) {
    throw new NotImplementedException();

Is it better to throw an exception instead of returning null? I think so, null is sneakier and has a better chance to slip away than an exception. If the exception never is thrown, it is just an indication that this particular code never is executed and could be removed.

Change the default IDE behaviour

I use IntelliJ IDEA and changing the default behaviour is done in

Preferences | File Templates | Code | Implemented Method Body and New Method Body

My new settings are

#if ( $RETURN_TYPE != "void" ) throw new NotImplementedException();#end



  1. Good point. But isn’t java.lang.UnsupportedOperation the standard exception to use for not implemented methods?

    Comment by Peter Koller — July 29, 2011 @ 18:42

    • You might be right. But then again, not supported isn’t really what I want to express. The operation is supposed to be supported, it just hasn’t been implemented yet.

      Comment by Thomas Sundberg — July 29, 2011 @ 19:22

  2. Thanks a lot.. Changing the IntelliJ behavior saved me lot of repetitive work🙂. You can remove the #if check, so that methods with void type also throw exception.

    Comment by tom — March 28, 2015 @ 01:56

    • Hi!

      You are absolutely right, removing the #if and keep the throw is a good suggestion.

      I took a look at my current Idea settings and realized that that is the exact implementation I use myself. So, I implement this as

      throw new NotImplementedException();

      and nothing more.


      Comment by Thomas Sundberg — March 28, 2015 @ 11:08

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: