Add some null checks.

Found by Coverity Scan.