Setting if statement to prevent a null reference