Add null check and try-catch and Log statements