Use Agent.getNextTargetId() method
There was a assert targetIds.size() > 0 That means, this case is not expected, right? Why is this case handled then in the next lines? I switched the assert statement against getNextTargetId() which throws an exception if there is no next target. Use hasNextTarget() if you have to check this.