Added null check to avoid null pointer exception