fix npe, and add tests for #739