stanfordnlp/CoreNLP · error · NullPointerException

Cannot handle null mode

Error message

Cannot handle null mode

What it means

ArrayStringFilter requires a filtering Mode (EXACT, PREFIX, or CASE_INSENSITIVE) to decide how words are matched. A null mode makes the filter's behavior undefined, so the constructor throws NullPointerException with the message 'Cannot handle null mode' rather than defaulting to a mode.

Solutions

  1. Pass an explicit ArrayStringFilter.Mode value (Mode.EXACT, Mode.PREFIX, or Mode.CASE_INSENSITIVE) to the constructor.
  2. If the mode comes from a string/config value, resolve it with Mode.valueOf(...) guarded by a null/default branch before construction.
  3. Null-check the mode variable at the call site and substitute a sensible default such as Mode.EXACT.

Example fix

// before
ArrayStringFilter filter = new ArrayStringFilter(getModeFromConfig(), "foo"); // may pass null
// after
ArrayStringFilter.Mode mode = getModeFromConfig();
if (mode == null) mode = ArrayStringFilter.Mode.EXACT;
ArrayStringFilter filter = new ArrayStringFilter(mode, "foo");
Defensive patterns

Strategy: validation

Validate before calling

if (mode == null) {
  mode = ArrayStringFilter.Mode.EXACT; // or reject early
}

Type guard

ArrayStringFilter.Mode safeMode(ArrayStringFilter.Mode m) { return m != null ? m : ArrayStringFilter.Mode.EXACT; }

Try / catch

try {
  ArrayStringFilter f = new ArrayStringFilter(mode, words);
} catch (NullPointerException e) {
  // fall back to a default-mode filter
  ArrayStringFilter f = new ArrayStringFilter(ArrayStringFilter.Mode.EXACT, words);
}

Prevention

When it happens

Trigger: Calling new ArrayStringFilter(null, ...) or passing a mode variable that was never assigned (e.g. a Mode field read from config before initialization, or a switch that returned null).

Common situations: Parsing a mode string from properties where the key is missing and a lookup returns null; accidentally passing null as the first varargs-style argument intending to pass only words; refactoring that removed a default mode constant.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/util/ArrayStringFilter.java:30

 * using regexes if the array of strings is small enough.  No specific
 * experiments exist for how long the array can be before performance
 * is worse than a regex, but the English dependencies code was helped
 * by replacing disjunction regexes of 6 words or fewer with this.
 *
 * @author John Bauer
 */
public class ArrayStringFilter implements Predicate<String>, Serializable {
  private final String[] words;
  private final int length;
  private final Mode mode;

  public enum Mode {
    EXACT, PREFIX, CASE_INSENSITIVE
  }

  public ArrayStringFilter(Mode mode, String ... words) {
    if (mode == null) {
      throw new NullPointerException("Cannot handle null mode");
    }
    this.mode = mode;
    this.words = new String[words.length];
    System.arraycopy(words, 0, this.words, 0, words.length);
    this.length = words.length;
  }

  @Override
  public boolean test(String input) {
    switch (mode) {
    case EXACT:
      for (int i = 0; i < length; ++i) {
        if (words[i].equals(input)) {
          return true;
        }
      }
      return false;
    case PREFIX:

View on GitHub (pinned to 1b7edd19c4)