stanfordnlp/CoreNLP · error · IllegalArgumentException
Cannot read from null object!
Error message
Cannot read from null object!
What it means
The DocumentPreprocessor constructor requires a non-null Reader to read the document from. Passing null input makes it impossible to produce any tokenized sentences, so the constructor immediately throws this IllegalArgumentException as a fail-fast guard.
Solutions
- Check why the Reader is null before constructing — log/verify the source that produced it
- Fix resource loading: use IOUtils.readerFromString/readerFromFile and handle IOException instead of a possibly-null stream
- Pass a valid reader, e.g. new DocumentPreprocessor(new BufferedReader(new FileReader(path)))
- Add an explicit null check on your input before calling the constructor
Example fix
// before
Reader r = MyClass.class.getResourceAsStream(path) != null
? new InputStreamReader(MyClass.class.getResourceAsStream(path)) : null;
DocumentPreprocessor dp = new DocumentPreprocessor(r);
// after
InputStream in = MyClass.class.getResourceAsStream(path);
if (in == null) throw new FileNotFoundException("Missing resource: " + path);
DocumentPreprocessor dp = new DocumentPreprocessor(new InputStreamReader(in, StandardCharsets.UTF_8)); Defensive patterns
Strategy: type-guard
Validate before calling
if (reader == null) {
throw new IllegalArgumentException("Document reader must not be null");
} Type guard
static Reader requireNonNullReader(Reader r) {
if (r == null) throw new IllegalArgumentException("Reader is null: source failed to load");
return r;
} Try / catch
try {
DocumentPreprocessor dp = new DocumentPreprocessor(reader);
} catch (IllegalArgumentException e) {
if ("Cannot read from null object!".equals(e.getMessage())) {
throw new DocumentLoadException("Input reader was null — check resource loading", e);
}
throw e;
} Prevention
- Never pass the result of getResourceAsStream()/a possibly-null Reader straight into the constructor
- Fail at the loading site with FileNotFoundException instead of propagating null
- Prefer IOUtils helpers that throw IOException over APIs that return null on missing resources
When it happens
Trigger: Calling new DocumentPreprocessor((Reader) null) or new DocumentPreprocessor(reader, DocType.Plain) with a null reader — commonly when a method that should have produced a Reader (e.g. IOUtils / getResourceAsStream / FileReader) returned null and the result is passed along unchecked.
Common situations: Classpath resource lookup returning null (getResourceAsStream on a missing resource); a file that failed to open silently mapped to null; a config field for the document source left unset.
Related errors
- You can't make a Tokenizer out of a null Lexer!
- Cannot open null document path!
- Attempt to open file with null name
- WordsToSentencesAnnotator: unable to find words/tokens in:
- argsToProperties could not read properties file: " + file
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/ae1761c7e3619398.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/process/DocumentPreprocessor.java:109
//From PTB conventions
private final String[] sentenceFinalFollowers = {")", "]", "}", "\"", "'", "''", "-RRB-", "-RSB-", "-RCB-"};
private boolean keepEmptySentences; // = false;
/**
* Constructs a preprocessor from an existing input stream.
*
* @param input An existing reader
*/
public DocumentPreprocessor(Reader input) {
this(input,DocType.Plain);
}
public DocumentPreprocessor(Reader input, DocType t) {
if (input == null) {
throw new IllegalArgumentException("Cannot read from null object!");
}
docType = t;
inputReader = input;
}
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.
*View on GitHub (pinned to 1b7edd19c4)