Fix exception thrown by findEndOfNode

Also rewrite init/while/increment as a for loop.

Bug: 60117
Change-Id: I17442499d3d2bd3fcb8710e238e10786779e4ee9