I think the problem is only that restnodes isn't properly loaded when called. It's either null or empty, which means there are no nodes in the group "nodes"
, in which case you should add a if statement to check if restnodes contains elements :
if rest_nodes.size() < 0):
Are you trying to get the position of an object? There is maybe an easier way to do so that doesn't require groups. If you say more about it, I could try to help you find a better replacement.