added null check and type casting