Updated code after removing null point exception