{"record":{"id":"ae1761c7e3619398","repo":"stanfordnlp/CoreNLP","slug":"cannot-read-from-null-object","errorCode":null,"errorMessage":"Cannot read from null object!","messagePattern":"Cannot read from null object!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/process/DocumentPreprocessor.java","lineNumber":109,"sourceCode":"\n  //From PTB conventions\n  private final String[] sentenceFinalFollowers = {\")\", \"]\", \"}\", \"\\\"\", \"'\", \"''\", \"-RRB-\", \"-RSB-\", \"-RCB-\"};\n\n  private boolean keepEmptySentences; // = false;\n\n\n  /**\n   * Constructs a preprocessor from an existing input stream.\n   *\n   * @param input An existing reader\n   */\n  public DocumentPreprocessor(Reader input) {\n    this(input,DocType.Plain);\n  }\n\n  public DocumentPreprocessor(Reader input, DocType t) {\n    if (input == null) {\n      throw new IllegalArgumentException(\"Cannot read from null object!\");\n    }\n    docType = t;\n    inputReader = input;\n  }\n\n  public DocumentPreprocessor(String docPath) {\n    this(docPath, DocType.Plain, \"UTF-8\");\n  }\n\n  public DocumentPreprocessor(String docPath, DocType t) {\n    this(docPath, t, \"UTF-8\");\n  }\n\n\n  /**\n   * Constructs a preprocessor from a file at a path, which can be either\n   * a filesystem location, a classpath entry, or a URL.\n   *","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/process/DocumentPreprocessor.java#L91-L127","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nReader r = MyClass.class.getResourceAsStream(path) != null\n    ? new InputStreamReader(MyClass.class.getResourceAsStream(path)) : null;\nDocumentPreprocessor dp = new DocumentPreprocessor(r);\n// after\nInputStream in = MyClass.class.getResourceAsStream(path);\nif (in == null) throw new FileNotFoundException(\"Missing resource: \" + path);\nDocumentPreprocessor dp = new DocumentPreprocessor(new InputStreamReader(in, StandardCharsets.UTF_8));","handlingStrategy":"type-guard","validationCode":"if (reader == null) {\n  throw new IllegalArgumentException(\"Document reader must not be null\");\n}","typeGuard":"static Reader requireNonNullReader(Reader r) {\n  if (r == null) throw new IllegalArgumentException(\"Reader is null: source failed to load\");\n  return r;\n}","tryCatchPattern":"try {\n  DocumentPreprocessor dp = new DocumentPreprocessor(reader);\n} catch (IllegalArgumentException e) {\n  if (\"Cannot read from null object!\".equals(e.getMessage())) {\n    throw new DocumentLoadException(\"Input reader was null — check resource loading\", e);\n  }\n  throw e;\n}","preventionTips":["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"],"tags":["null","constructor","io","illegal-argument","precondition"],"backgroundTag":"null-argument","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}