{"record":{"id":"c0a1c38f789e3e46","repo":"pentaho/pentaho-kettle","slug":"mailconnection-error-fetchingmessages","errorCode":null,"errorMessage":"MailConnection.Error.FetchingMessages","messagePattern":"MailConnection\\.Error\\.FetchingMessages","errorType":"exception","errorClass":"KettleException","httpStatus":null,"severity":"error","filePath":"plugins/email-messages/impl/src/main/java/org/pentaho/di/job/entries/getpop/MailConnection.java","lineNumber":1238,"sourceCode":"  private void updateMessageNr() {\n    this.messagenr++;\n  }\n\n  private int getMessageNr() {\n    return this.messagenr;\n  }\n\n  /**\n   * Get next message.\n   *\n   * @throws KettleException\n   */\n  public void fetchNext() throws KettleException {\n    updateMessageNr();\n    try {\n      this.message = this.messages[getMessageNr()];\n    } catch ( Exception e ) {\n      throw new KettleException( BaseMessages.getString( PKG, \"MailConnection.Error.FetchingMessages\" ), e );\n    }\n  }\n\n  /**\n   * Returns the current message.\n   *\n   * @return current message\n   */\n  public Message getMessage() {\n    return this.message;\n  }\n\n  /**\n   * Returns the number of messages.\n   *\n   * @return messages count\n   */\n  public int getMessagesCount() {","sourceCodeStart":1220,"sourceCodeEnd":1256,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/plugins/email-messages/impl/src/main/java/org/pentaho/di/job/entries/getpop/MailConnection.java#L1220-L1256","documentation":"MailConnection.fetchNext() wraps any exception from indexing this.messages[getMessageNr()] into a KettleException with key MailConnection.Error.FetchingMessages. The typical cause is an ArrayIndexOutOfBoundsException when the internal counter runs past the end of the fetched message array, or a Message has been expunged.","triggerScenarios":"Calling fetchNext() more times than there are messages in the folder (getMessageNr() >= messages.length), or after the folder's messages were expunged so the array/sequence numbers are stale.","commonSituations":"Loop boundary off-by-one in a custom job/script driving MailConnection directly; another client deleted messages mid-scan making the cached array stale; folder re-opened and messages refetched with a different count.","solutions":["Stop iterating when getMessageNr() >= getMessagesCount() instead of relying on fetchNext() to throw.","Use the library's iteration loop (nextMessage/hasMore style helpers) rather than manual index arithmetic.","Re-fetch the message list after external deletions, then restart the iteration."],"exampleFix":"// before\nwhile (true) {\n  connection.fetchNext(); // eventually out of bounds\n  process(connection.getMessage());\n}\n// after\nfor (int i = 0; i < connection.getMessagesCount(); i++) {\n  connection.fetchNext();\n  process(connection.getMessage());\n}","handlingStrategy":"validation","validationCode":"if (connection.getMessagesCount() == 0 || connection.getMessageNr() >= connection.getMessagesCount()) { return; // nothing left to fetch }","typeGuard":null,"tryCatchPattern":"try { connection.fetchNext(); } catch (KettleException e) { logError(\"Fetch failed at index \" + connection.getMessageNr(), e); break; }","preventionTips":["Bound iteration loops with getMessagesCount()","Refetch messages after external expunges","Prefer the library's iteration helpers over manual indexing"],"tags":["javamail","iteration","index-out-of-bounds"],"backgroundTag":"index-out-of-bounds","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}