{"record":{"id":"a769f3795ff07ce6","repo":"stanfordnlp/CoreNLP","slug":"collection-elements-must-be-files-or-strings","errorCode":null,"errorMessage":"Collection elements must be Files or Strings","messagePattern":"Collection elements must be Files or Strings","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/FileSequentialCollection.java","lineNumber":330,"sourceCode":"            Integer index = fileArrayStackIndices.pop();\n            int ind = index.intValue();\n            if (ind < files.length) {\n              index = Integer.valueOf(ind + 1);\n              fileArrayStackIndices.push(index);\n              fileArrayStack.push(files[ind]);\n              // loop around to process this new file\n            } else {\n              // this directory is finished and we pop up\n              fileArrayStack.pop();\n            }\n          } else {\n            // take it off the stack: tail recursion optimization\n            fileArrayStack.pop();\n            if (obj instanceof String) {\n              obj = new File((String) obj);\n            }\n            if (!(obj instanceof File)) {\n              throw new IllegalArgumentException(\"Collection elements must be Files or Strings\");\n            }\n            File path = (File) obj;\n            if (path.isDirectory()) {\n              // log.info(\"Got directory \" + path);\n              // if path is a directory, look into it\n              File[] directoryListing = path.listFiles(filt);\n              if (directoryListing == null) {\n                throw new IllegalArgumentException(\"Directory access problem for: \" + path);\n              }\n              // log.info(\"  with \" +\n              //\t    directoryListing.length + \" files in it.\");\n              if (includeDirs) {\n                // log.info(\"Include dir as answer\");\n                if (directoryListing.length > 0) {\n                  fileArrayStack.push(directoryListing);\n                  fileArrayStackIndices.push(Integer.valueOf(0));\n                }\n                return path;","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/FileSequentialCollection.java#L312-L348","documentation":"FileSequentialCollection lazily walks a directory tree and accepts a seed collection whose elements may be Files or Strings (paths). primeNextFile validates each popped element and throws IllegalArgumentException when an element is neither a File nor a String, because it cannot be interpreted as a path.","triggerScenarios":"Constructing a FileSequentialCollection (or iterating it via its iterator/next()) with a Collection containing objects of an unexpected type, e.g. Path, URI, Integer, or null elements.","commonSituations":"Passing a List<Path> from java.nio instead of File/String; passing URL or URI objects; mixing results of another API (e.g. file resolver output) into the seed collection; null entries in the collection.","solutions":["Convert every seed element to File or String before constructing the collection, e.g. path.toFile() for java.nio Paths.","Only add File or String instances to the collection; filter or reject other types up front.","Check for and remove null entries from the input collection.","If using Path objects, call Paths/Path.toFile() or toString() first."],"exampleFix":"// before\ncollection.add(myPath); // java.nio.file.Path\n// after\ncollection.add(myPath.toFile());","handlingStrategy":"type-guard","validationCode":"for (Object o : seeds) {\n  if (!(o instanceof File) && !(o instanceof String)) {\n    throw new IllegalArgumentException(\"Seed element must be File or String: \" + (o == null ? \"null\" : o.getClass()));\n  }\n}","typeGuard":"boolean isValidSeed(Object o) {\n  return o instanceof File || o instanceof String;\n}","tryCatchPattern":"try {\n  FileSequentialCollection c = new FileSequentialCollection(seeds, filt, includeDirs);\n  for (File f : c) { process(f); }\n} catch (IllegalArgumentException e) {\n  log.severe(\"Bad seed element: \" + e.getMessage());\n}","preventionTips":["Only insert File or String into seed collections","Convert java.nio Paths with toFile() at the boundary","Reject nulls when populating the collection"],"tags":["file-io","argument-validation","classpath"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}