Fix several sources of NullPointerExceptions and unnecessary stack traces