stanfordnlp/CoreNLP · error · IllegalArgumentException

Collection elements must be Files or Strings

Error message

Collection elements must be Files or Strings

What it means

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.

Solutions

  1. Convert every seed element to File or String before constructing the collection, e.g. path.toFile() for java.nio Paths.
  2. Only add File or String instances to the collection; filter or reject other types up front.
  3. Check for and remove null entries from the input collection.
  4. If using Path objects, call Paths/Path.toFile() or toString() first.

Example fix

// before
collection.add(myPath); // java.nio.file.Path
// after
collection.add(myPath.toFile());
Defensive patterns

Strategy: type-guard

Validate before calling

for (Object o : seeds) {
  if (!(o instanceof File) && !(o instanceof String)) {
    throw new IllegalArgumentException("Seed element must be File or String: " + (o == null ? "null" : o.getClass()));
  }
}

Type guard

boolean isValidSeed(Object o) {
  return o instanceof File || o instanceof String;
}

Try / catch

try {
  FileSequentialCollection c = new FileSequentialCollection(seeds, filt, includeDirs);
  for (File f : c) { process(f); }
} catch (IllegalArgumentException e) {
  log.severe("Bad seed element: " + e.getMessage());
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/a769f3795ff07ce6. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/io/FileSequentialCollection.java:330

            Integer index = fileArrayStackIndices.pop();
            int ind = index.intValue();
            if (ind < files.length) {
              index = Integer.valueOf(ind + 1);
              fileArrayStackIndices.push(index);
              fileArrayStack.push(files[ind]);
              // loop around to process this new file
            } else {
              // this directory is finished and we pop up
              fileArrayStack.pop();
            }
          } else {
            // take it off the stack: tail recursion optimization
            fileArrayStack.pop();
            if (obj instanceof String) {
              obj = new File((String) obj);
            }
            if (!(obj instanceof File)) {
              throw new IllegalArgumentException("Collection elements must be Files or Strings");
            }
            File path = (File) obj;
            if (path.isDirectory()) {
              // log.info("Got directory " + path);
              // if path is a directory, look into it
              File[] directoryListing = path.listFiles(filt);
              if (directoryListing == null) {
                throw new IllegalArgumentException("Directory access problem for: " + path);
              }
              // log.info("  with " +
              //	    directoryListing.length + " files in it.");
              if (includeDirs) {
                // log.info("Include dir as answer");
                if (directoryListing.length > 0) {
                  fileArrayStack.push(directoryListing);
                  fileArrayStackIndices.push(Integer.valueOf(0));
                }
                return path;

View on GitHub (pinned to 1b7edd19c4)