Nulls & Null Objects

Excessive null checking can be alleviated by the use of the Null Object Pattern. Rather than passing a null value a object that represents the null state can be provided with sensible values.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
  public interface Animal {
      void makeSound();
  }

  public class Dog implements Animal {
      public void makeSound() {
          System.out.println("woof!");
      }
  }

  public class NullAnimal implements Animal {
      public void makeSound() {

      }
  }

References

Null Object