Fix check if result is null before cast