Added null reference check