null check and error logging for __toString errors