1006 Added extra guards against NPE

This is for issue #1006
@crankycoder r?