{"record":{"id":"fa1bd3a6515aaed1","repo":"stanfordnlp/CoreNLP","slug":"encoding-must-not-be-null","errorCode":null,"errorMessage":"encoding must not be null","messagePattern":"encoding must not be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/ReaderInputStream.java","lineNumber":60,"sourceCode":"   *\n   * @param reader   <CODE>Reader</CODE>.  Must not be <code>null</code>.\n   */\n  public ReaderInputStream(Reader reader) {\n    in = reader;\n  }\n\n  /**\n   * Construct a <CODE>ReaderInputStream</CODE>\n   * for the specified <CODE>Reader</CODE>,\n   * with the specified encoding.\n   *\n   * @param reader     non-null <CODE>Reader</CODE>.\n   * @param encoding   non-null <CODE>String</CODE> encoding.\n   */\n  public ReaderInputStream(Reader reader, String encoding) {\n    this(reader);\n    if (encoding == null) {\n      throw new IllegalArgumentException(\"encoding must not be null\");\n    } else {\n      this.encoding = encoding;\n    }\n  }\n\n  /**\n   * Reads from the <CODE>Reader</CODE>, returning the same value.\n   *\n   * @return the value of the next character in the <CODE>Reader</CODE>.\n   *\n   * @exception IOException if the original <code>Reader</code> fails to be read\n   */\n  public synchronized int read() throws IOException {\n    if (in == null) {\n      throw new IOException(\"Stream Closed\");\n    }\n\n    byte result;","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/ReaderInputStream.java#L42-L78","documentation":"ReaderInputStream(Reader, String) throws IllegalArgumentException when the encoding parameter is null. The adapter wraps a character Reader as a byte InputStream, which requires a non-null charset name to encode characters. This is an eager argument check so construction fails immediately instead of later during read.","triggerScenarios":"Calling new ReaderInputStream(reader, null); typically when the charset name is passed through from a nullable variable (e.g. a missing file.encoding property or an unset config value).","commonSituations":"Reading a file or stream with an encoding loaded from properties/env that was never set; refactored code where an overload defaulted the encoding but the two-arg constructor is now used; null charset strings from serialization or user config.","solutions":["Pass a concrete encoding string such as \"UTF-8\" or the platform default Charset.defaultCharset().name().","If the encoding may be absent, use a fallback before calling: encoding != null ? encoding : \"UTF-8\".","Use the single-arg constructor new ReaderInputStream(reader) if no specific encoding is needed."],"exampleFix":"// before\nInputStream in = new ReaderInputStream(reader, props.getProperty(\"encoding\"));\n// after\nString encoding = props.getProperty(\"encoding\");\nif (encoding == null) encoding = \"UTF-8\";\nInputStream in = new ReaderInputStream(reader, encoding);","handlingStrategy":"validation","validationCode":"if (encoding == null || encoding.isEmpty()) encoding = \"UTF-8\";\nInputStream in = new ReaderInputStream(reader, encoding);","typeGuard":"String safeEncoding = encoding != null ? encoding : \"UTF-8\";","tryCatchPattern":"try {\n  in = new ReaderInputStream(reader, encoding);\n} catch (IllegalArgumentException e) {\n  in = new ReaderInputStream(reader, \"UTF-8\");\n}","preventionTips":["Never pass a raw property/env value as an encoding without a null/default check.","Centralize charset resolution in one utility that always returns a non-null value.","Prefer Charset objects over raw strings where the API allows."],"tags":["null-argument","io","encoding","input-stream"],"backgroundTag":"null-argument","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}