stanfordnlp/CoreNLP · error · NullPointerException

Attempt to open file with null name

Error message

Attempt to open file with null name

What it means

IOUtils.getInputStreamFromURLOrClasspathOrFileSystem() resolves a resource by URL, classpath, or filesystem path. It throws NullPointerException immediately if the given name is null, since nothing can be resolved.

Solutions

  1. Validate the input is non-null before calling; fail with a clear message naming the missing config/flag.
  2. Provide the required value via -D property, environment variable, or option.
  3. Use Objects.requireNonNull(value, "which key") at the configuration boundary to catch it early.
  4. If the value is genuinely optional, guard the call: if (name != null) ...

Example fix

// before
InputStream in = IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(props.getProperty("model"));
// after
String modelPath = props.getProperty("model");
Objects.requireNonNull(modelPath, "Missing required property: model");
InputStream in = IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(modelPath);
Defensive patterns

Strategy: validation

Validate before calling

if (name == null || name.trim().isEmpty()) {
  throw new IllegalArgumentException("Resource name must be provided");
}

Type guard

boolean isValidName(String s) {
  return s != null && !s.trim().isEmpty();
}

Try / catch

try {
  InputStream in = IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(name);
} catch (NullPointerException e) {
  log.severe("Null resource name; check config key 'model' and CLI options");
}

Prevention

When it happens

Trigger: Passing a null String to getInputStreamFromURLOrClasspathOrFileSystem (or its wrappers getDataInputStream, readObjectFromURLOrClasspathOrFileSystem, readerFromString), typically because a config property, argument, or System.getProperty lookup returned null.

Common situations: Missing command-line option or properties key; env var not set and read via System.getenv returning null; optional config field not provided; API default changed between library versions.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/io/IOUtils.java:485

    InputStream is = findStreamInClassLoader(name);
    return is != null || new File(name).exists();
  }

  /**
   * Locates this file either using the given URL, or in the CLASSPATH, or in the file system
   * The CLASSPATH takes priority over the file system!
   * This stream is buffered and gunzipped (if necessary).
   *
   * @param textFileOrUrl The String specifying the URL/resource/file to load
   * @return An InputStream for loading a resource
   * @throws IOException On any IO error
   * @throws NullPointerException Input parameter is null
   */
  public static InputStream getInputStreamFromURLOrClasspathOrFileSystem(String textFileOrUrl)
          throws IOException, NullPointerException {
    InputStream in;
    if (textFileOrUrl == null) {
      throw new NullPointerException("Attempt to open file with null name");
    } else if (textFileOrUrl.matches("https?://.*")) {
      URL u = new URL(textFileOrUrl);
      URLConnection uc = u.openConnection();
      in = uc.getInputStream();
    } else {
      try {
        in = findStreamInClasspathOrFileSystem(textFileOrUrl);
      } catch (FileNotFoundException e) {
        try {
          // Maybe this happens to be some other format of URL?
          URL u = new URL(textFileOrUrl);
          URLConnection uc = u.openConnection();
          in = uc.getInputStream();
        } catch (IOException e2) {
          // Don't make the original exception a cause, since it is usually bogus
          throw new IOException("Unable to open \"" +
                  textFileOrUrl + "\" as " + "class path, filename or URL"); // , e2);
        }

View on GitHub (pinned to 1b7edd19c4)