{"record":{"id":"79b6d1932fdf56e5","repo":"pentaho/pentaho-kettle","slug":"textfileinputdialog-exception-errorgettingfirstlines","errorCode":null,"errorMessage":"TextFileInputDialog.Exception.ErrorGettingFirstLines","messagePattern":"TextFileInputDialog\\.Exception\\.ErrorGettingFirstLines","errorType":"exception","errorClass":"KettleException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/fileinput/text/TextFileInputHelper.java","lineNumber":333,"sourceCode":"    StringBuilder lineStringBuilder = new StringBuilder( 256 );\n    try ( InputStream fi = KettleVFS.getInputStream( file ) ) {\n      CompressionProvider provider = CompressionProviderFactory.getInstance()\n        .createCompressionProviderInstance( meta.content.fileCompression );\n      f = provider.createInputStream( fi );\n      try (BufferedInputStreamReader reader =\n             (meta.getEncoding() != null && !meta.getEncoding().isEmpty())\n               ? new BufferedInputStreamReader(new InputStreamReader(f, meta.getEncoding()))\n               : new BufferedInputStreamReader(new InputStreamReader(f))) {\n\n        EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());\n        int maxnr = nrlines + (meta.content.header ? meta.content.nrHeaderLines : 0);\n        if (skipHeaders) {\n          skipHeaderLines(meta, reader, encodingType, lineStringBuilder);\n        }\n        readFileLines(meta, retval, reader, encodingType, nrlines, maxnr, lineStringBuilder);\n      }\n    } catch ( Exception e ) {\n      throw new KettleException(\n        BaseMessages.getString( PKG, \"TextFileInputDialog.Exception.ErrorGettingFirstLines\", \"\" + nrlines,\n          file.getName().getURI() ), e );\n    } finally {\n      try {\n        if ( f != null ) {\n          f.close();\n        }\n      } catch ( Exception e ) {\n        // Ignore errors\n      }\n    }\n\n    return retval;\n  }\n\n  /**\n   * Skips the required header lines if present.\n   */","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/fileinput/text/TextFileInputHelper.java#L315-L351","documentation":"getFirst() reads the first N lines of a text file (for sampling/preview) and wraps any underlying I/O, decoding, or parsing failure in a KettleException with the message 'ErrorGettingFirstLines', including the requested line count and the file URI. The library throws it because the caller (dialog preview, content guessing) cannot proceed without sample lines. The original exception is chained as the cause.","triggerScenarios":"Calling TextFileInputHelper.getFirst(...) (directly or via rows()/content()) when the file cannot be opened or read: missing file, bad VFS URI, wrong encoding, or an IOException/RuntimeException thrown inside skipHeaderLines/readFileLines.","commonSituations":"Previewing a CSV in Spoon whose file was moved or deleted; a VFS URI with typos (sftp://, s3:// credentials missing); a file with an encoding different from the one configured causing decode exceptions.","solutions":["Verify the file exists and the URI is reachable (open it manually or with a file browser before previewing).","Check VFS credentials/scheme configuration for remote files (SFTP, S3, HTTP).","Try re-reading with the correct character encoding in the Content tab.","Open the chained cause (e.getCause()) to identify the root IO/decode failure and fix that.","Increase logging to TRACE on org.pentaho.di.trans.steps.fileinput to see which line/read failed."],"exampleFix":"// before: assuming the file is always readable\nString[] lines = TextFileInputHelper.getFirst(log, meta, 100, transMeta, null);\n// after: guard existence first\nFileObject f = KettleVFS.getFileObject(meta.getFileName(transMeta), transMeta);\nif (!f.exists()) { throw new KettleFileException(\"File not found: \" + f.getName().getURI()); }\nString[] lines = TextFileInputHelper.getFirst(log, meta, 100, transMeta, null);","handlingStrategy":"validation","validationCode":"FileObject f = KettleVFS.getFileObject(filename, transMeta);\nif (!f.exists() || !f.isReadable()) {\n  throw new KettleFileException(\"Cannot read file: \" + f.getName().getURI());\n}","typeGuard":"if (meta == null || meta.inputFiles == null || meta.inputFiles.fileName == null || meta.inputFiles.fileName.length == 0) return false; // file config not ready for getFirst","tryCatchPattern":"try {\n  String[] lines = TextFileInputHelper.getFirst(log, meta, 100, transMeta, null);\n} catch (KettleException e) {\n  log.logError(\"Preview failed for \" + e.getCause(), e);\n  // fall back to manual line reading or abort preview\n}","preventionTips":["Always check file existence/readability via VFS before preview operations.","Validate remote file credentials (SFTP/S3) before running transformations.","Keep configured encoding consistent with the file's actual encoding.","Log the file URI from the exception message when triaging."],"tags":["kettle","etl","file-input","io-exception"],"backgroundTag":"file-read-failed","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"}