stanfordnlp/CoreNLP · error · IllegalArgumentException

Cannot open null document path!

Error message

Cannot open null document path!

What it means

The DocumentPreprocessor(String docPath, DocType, String encoding) constructor refuses a null docPath with this IllegalArgumentException. Without a path there is no file to open, so the constructor fails fast before attempting IO.

Solutions

  1. Ensure the docPath variable is populated before construction (check CLI args / config loading)
  2. Provide a default path or fail earlier with a clear message when the path option is absent
  3. Use the Reader-based constructor instead if you already have the content in memory
  4. Add a null/empty check on the path at the boundary of your program and report a user-friendly error

Example fix

// before
DocumentPreprocessor dp = new DocumentPreprocessor(opts.docPath, DocType.Plain, "UTF-8");
// after
if (opts.docPath == null) {
  throw new IllegalArgumentException("Usage error: --docPath is required");
}
DocumentPreprocessor dp = new DocumentPreprocessor(opts.docPath, DocType.Plain, "UTF-8");
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(docPath, "docPath must be set (CLI --docPath or config)");
if (docPath.trim().isEmpty()) throw new IllegalArgumentException("docPath is empty");

Type guard

static String requirePath(String p) {
  if (p == null || p.trim().isEmpty()) throw new IllegalArgumentException("Document path missing");
  return p;
}

Try / catch

try {
  DocumentPreprocessor dp = new DocumentPreprocessor(docPath, DocType.Plain, "UTF-8");
} catch (IllegalArgumentException e) {
  if ("Cannot open null document path!".equals(e.getMessage())) {
    throw new UsageException("No input document path supplied", e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling new DocumentPreprocessor(null, DocType.Plain, "UTF-8") or any variant of the path-based constructor with a null path — e.g. when the path comes from a config option, CLI argument, or method parameter that was never set.

Common situations: Missing command-line/config value for the input file; code path that conditionally sets the document path but the condition did not hold; property loaded from an incomplete configuration file.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/process/DocumentPreprocessor.java:133

  public DocumentPreprocessor(String docPath) {
    this(docPath, DocType.Plain, "UTF-8");
  }

  public DocumentPreprocessor(String docPath, DocType t) {
    this(docPath, t, "UTF-8");
  }


  /**
   * Constructs a preprocessor from a file at a path, which can be either
   * a filesystem location, a classpath entry, or a URL.
   *
   * @param docPath The path
   * @param encoding The character encoding used by Readers
   */
  public DocumentPreprocessor(String docPath, DocType t, String encoding) {
    if (docPath == null) {
      throw new IllegalArgumentException("Cannot open null document path!");
    }

    docType = t;
    try {
      inputReader = IOUtils.readerFromString(docPath, encoding);
    } catch (IOException ioe) {
      throw new RuntimeIOException(String.format("%s: Could not open path %s", this.getClass().getName(), docPath),
              ioe);
    }
  }

  /**
   * Set whether or not the tokenizer keeps empty sentences in
   * whitespace mode.  Useful for programs that want to echo blank
   * lines.  Not relevant for the non-whitespace model.
   */
  public void setKeepEmptySentences(boolean keepEmptySentences) {
    this.keepEmptySentences = keepEmptySentences;

View on GitHub (pinned to 1b7edd19c4)