Fix incorrectly throwing NoSuchElementExceptions (#12524)

Change-Id: Ie18e900f0fb3b092cb5fd81516da4610c1e419ed