stanfordnlp/CoreNLP · error · IllegalArgumentException

encoding must not be null

Error message

encoding must not be null

What it means

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.

Solutions

  1. Pass a concrete encoding string such as "UTF-8" or the platform default Charset.defaultCharset().name().
  2. If the encoding may be absent, use a fallback before calling: encoding != null ? encoding : "UTF-8".
  3. Use the single-arg constructor new ReaderInputStream(reader) if no specific encoding is needed.

Example fix

// before
InputStream in = new ReaderInputStream(reader, props.getProperty("encoding"));
// after
String encoding = props.getProperty("encoding");
if (encoding == null) encoding = "UTF-8";
InputStream in = new ReaderInputStream(reader, encoding);
Defensive patterns

Strategy: validation

Validate before calling

if (encoding == null || encoding.isEmpty()) encoding = "UTF-8";
InputStream in = new ReaderInputStream(reader, encoding);

Type guard

String safeEncoding = encoding != null ? encoding : "UTF-8";

Try / catch

try {
  in = new ReaderInputStream(reader, encoding);
} catch (IllegalArgumentException e) {
  in = new ReaderInputStream(reader, "UTF-8");
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/io/ReaderInputStream.java:60

   *
   * @param reader   <CODE>Reader</CODE>.  Must not be <code>null</code>.
   */
  public ReaderInputStream(Reader reader) {
    in = reader;
  }

  /**
   * Construct a <CODE>ReaderInputStream</CODE>
   * for the specified <CODE>Reader</CODE>,
   * with the specified encoding.
   *
   * @param reader     non-null <CODE>Reader</CODE>.
   * @param encoding   non-null <CODE>String</CODE> encoding.
   */
  public ReaderInputStream(Reader reader, String encoding) {
    this(reader);
    if (encoding == null) {
      throw new IllegalArgumentException("encoding must not be null");
    } else {
      this.encoding = encoding;
    }
  }

  /**
   * Reads from the <CODE>Reader</CODE>, returning the same value.
   *
   * @return the value of the next character in the <CODE>Reader</CODE>.
   *
   * @exception IOException if the original <code>Reader</code> fails to be read
   */
  public synchronized int read() throws IOException {
    if (in == null) {
      throw new IOException("Stream Closed");
    }

    byte result;

View on GitHub (pinned to 1b7edd19c4)