Add null pointer checks and associated test cases.
Address review comments