fix null pointer exception

fix null pointer exception which sometimes makes the application crash