Is it not because you create an iterator two times: First
if (results.getNodes() != null && results.getNodes().hasNext()) {
and then
NodeIterator it = results.getNodes();
I would try to create it only once like this:
QueryResult results = query.execute();
NodeIterator it = results.getNodes();
if (it.hasNext()) {
...
}
Looking at source of SimpleQueryResult, it's doing something strange , the instance variable rowIterator is set to null, so the next time getRows is called you'll go the else part of the condition and you'll have the error you mentionned:
public synchronized RowIterator getRows() throws RepositoryException {
if (rowIterator != null) {
RowIterator iterator = rowIterator;
rowIterator = null;
return iterator;
} else {
throw new RepositoryException(
"This query result has already been iterated through");
}
}