Adding null checks to fix NPE.

fixes rSmart/issues#266