Why the iterator is exhausted before you call next()
The error fires at the moment you call next() on a FileIterator or FolderIterator that has no more items to yield. The Drive API does not return null — it throws. So if your iterator was already empty (zero results), the very first next() call crashes.
The most common reason the iterator is empty: you passed a partial name to getFilesByName(). That method does a strict equality match on the file title. A file named "Q4 Report Final.xlsx" will not appear in getFilesByName('Q4 Report'). The iterator comes back empty, you skip the hasNext() guard because you assumed the file exists, and the crash follows immediately.
I hit this the first time I was automating a folder of monthly invoices whose names all included a shared prefix. The files were plainly visible in Drive; the script crashed every run. The fix took two minutes once I understood the method is exact-match only.