Sunday, September 5, 2010

How to protect from Nulls in Java

(with thanks to Andrew Newman for a lot of these ideas)


How to protect from Nulls...
Each public method in each class rejects any null values with an IllegalArgumentException.
This is enforced by test cases - each test case extends from a common TestCase that checks public methods by ensuring they throw exception when null is passed in.

  • No objects exist with nulls as values.
  • All classes have default values (objects) that can be persisted to the Database. Have default values like "". Or optional values (like something not yet done) are separate relations.
Null Value Object and Serialization

http://www.javalobby.org/java/forums/t17491.html

class Xxx {
    public static EMPTY_XXX = new Xxx();
    ...
    private Object readResolve() {
        return EMPTY_XXX;
    }
}


You should write code that checks that == works - as a suite of tests too. Like AssertSerialization.

Null-safe operators in Java 7: