handle cursor == null correctly, trying to close it lead to NPE