Preventing nullpointer exception