Added null pointer checks