Check NPE, and improve error strings.

Fix #1152