added null check for cursor