Fix a number of potential NPEs.

Also add validation @Nullable annotations to hint static code analysis and users.