stanfordnlp/CoreNLP · error · java.lang.RuntimeException

don't know how to get Reader from class

Error message

don't know how to get Reader from class {class} of object {object}

What it means

ReaderIteratorFactory.setNextObject converts each element of the supplied input collection into a Reader. It supports File, String (path), URL, InputStream, and Reader; if an element is of any other type, it throws this RuntimeException telling you the class it cannot handle.

Solutions

  1. Convert unsupported elements to java.io.File or URL before adding them: file.toFile() or uri.toURL()
  2. Pass Strings (file paths) or Readers/InputStreams instead
  3. Filter the collection to supported types (File, URL, String, InputStream, Reader)

Example fix

// before
inputs.add(Paths.get("data.txt"));
// after
inputs.add(Paths.get("data.txt").toFile());
Defensive patterns

Strategy: type-guard

Validate before calling

List<Object> supported = inputs.stream()
  .map(o -> o instanceof Path ? ((Path) o).toFile() : o instanceof URI ? ((URI) o).toURL() : o)
  .collect(Collectors.toList());

Type guard

boolean isSupportedInput(Object o) { return o instanceof File || o instanceof URL || o instanceof String || o instanceof InputStream || o instanceof Reader; }

Try / catch

try { it = factory.getReaderIterator(); } catch (RuntimeException e) { if (e.getMessage().startsWith("don't know how to get Reader from class")) { /* normalize inputs to File/URL/String */ } else throw e; }

Prevention

When it happens

Trigger: Passing a List/set to a ReaderIteratorFactory (or getReaderIterator) containing objects of an unsupported type, e.g. Path, URI, or a custom file-wrapper object.

Common situations: Java 7+ code putting java.nio.file.Path objects in the collection (only java.io.File is supported); passing URIs instead of URLs; a refactor that changed the collection's element type.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/objectbank/ReaderIteratorFactory.java:217

//               iter = l.iterator();
//               file = (File) iter.next();
//             }
//             if (((String)o).endsWith(".gz")) {
//               BufferedReader tmp = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)), enc));
//               nextObject = tmp;
//             } else {
//               nextObject = new BufferedReader(new EncodingFileReader(file, enc));
//             }
//           } else {
            nextObject = new BufferedReader(new StringReader((String) o));
//          }
        } else if (o instanceof URL) {
          // todo: add encoding specification to this as well? -akleeman
          nextObject = new BufferedReader(new InputStreamReader(((URL) o).openStream()));
        } else if (o instanceof Reader) {
          nextObject = new BufferedReader((Reader) o);
        } else {
          throw new RuntimeException("don't know how to get Reader from class " + o.getClass() + " of object " + o);
        }
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }

    /**
     * @return true if there is another (valid) input source to read from
     */
    @Override
    public boolean hasNext() {
      return nextObject != null;
    }

    /**
     * Returns nextObject and then sets nextObject to the next input source.
     *
     * @return BufferedReader for next input source.

View on GitHub (pinned to 1b7edd19c4)