{"record":{"id":"71e2420d9c7e9390","repo":"stanfordnlp/CoreNLP","slug":"cannot-handle-null-mode","errorCode":null,"errorMessage":"Cannot handle null mode","messagePattern":"Cannot handle null mode","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/ArrayStringFilter.java","lineNumber":30,"sourceCode":" * using regexes if the array of strings is small enough.  No specific\n * experiments exist for how long the array can be before performance\n * is worse than a regex, but the English dependencies code was helped\n * by replacing disjunction regexes of 6 words or fewer with this.\n *\n * @author John Bauer\n */\npublic class ArrayStringFilter implements Predicate<String>, Serializable {\n  private final String[] words;\n  private final int length;\n  private final Mode mode;\n\n  public enum Mode {\n    EXACT, PREFIX, CASE_INSENSITIVE\n  }\n\n  public ArrayStringFilter(Mode mode, String ... words) {\n    if (mode == null) {\n      throw new NullPointerException(\"Cannot handle null mode\");\n    }\n    this.mode = mode;\n    this.words = new String[words.length];\n    System.arraycopy(words, 0, this.words, 0, words.length);\n    this.length = words.length;\n  }\n\n  @Override\n  public boolean test(String input) {\n    switch (mode) {\n    case EXACT:\n      for (int i = 0; i < length; ++i) {\n        if (words[i].equals(input)) {\n          return true;\n        }\n      }\n      return false;\n    case PREFIX:","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/ArrayStringFilter.java#L12-L48","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass an explicit ArrayStringFilter.Mode value (Mode.EXACT, Mode.PREFIX, or Mode.CASE_INSENSITIVE) to the constructor.","If the mode comes from a string/config value, resolve it with Mode.valueOf(...) guarded by a null/default branch before construction.","Null-check the mode variable at the call site and substitute a sensible default such as Mode.EXACT."],"exampleFix":"// before\nArrayStringFilter filter = new ArrayStringFilter(getModeFromConfig(), \"foo\"); // may pass null\n// after\nArrayStringFilter.Mode mode = getModeFromConfig();\nif (mode == null) mode = ArrayStringFilter.Mode.EXACT;\nArrayStringFilter filter = new ArrayStringFilter(mode, \"foo\");","handlingStrategy":"validation","validationCode":"if (mode == null) {\n  mode = ArrayStringFilter.Mode.EXACT; // or reject early\n}","typeGuard":"ArrayStringFilter.Mode safeMode(ArrayStringFilter.Mode m) { return m != null ? m : ArrayStringFilter.Mode.EXACT; }","tryCatchPattern":"try {\n  ArrayStringFilter f = new ArrayStringFilter(mode, words);\n} catch (NullPointerException e) {\n  // fall back to a default-mode filter\n  ArrayStringFilter f = new ArrayStringFilter(ArrayStringFilter.Mode.EXACT, words);\n}","preventionTips":["Never read Mode straight from config; parse with a null-safe valueOf wrapper that applies a default.","Keep mode constants referenced from Mode itself rather than local copies that can be null."],"tags":["java","null-argument","filter","constructor"],"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"}