Null Objects

A null object is a regular object that behaves in a deterministically boring way. For instance the EMPTY_MAP, EMPTY_LIST and EMPTY_SET in java.util.Collections are null object implementations of the various collection interfaces.

The point of a null object is that you can pass it around, use it, dereference it etc. just like you would a regular object. If you use them pervasively, they remove the need to ever check for null in your code.

XJB uses null objects liberally for things like the container-managed Transaction and TransactionPolicyHandler. The typical pattern is to have an immutable field in the interface definition called NULL.

We are using the null object implementation from ProxyToys, so the usage looks like:

public interface Transaction {
    Transaction NULL = (Transaction)Null.object(Transaction.class);
}
    

You'll see these scattered all over the code base - they are a very useful habit to get into.

TODO - talk about Null DataSource and registering DataSources with xjb-config.xml

Document History