Check cursor null or not before closing it