avoiding a null pointer exception due wrong order of statements