stanfordnlp/CoreNLP · error · IllegalArgumentException

Value cannot be null

Error message

Value cannot be null

What it means

TrieMap.put rejects null values with IllegalArgumentException('Value cannot be null'). TrieMap uses a null value internally to mark non-terminal nodes, so storing an actual null entry is ambiguous and unsupported.

Solutions

  1. Filter out entries with null values before put/putAll.
  2. Replace null values with a sentinel object (e.g., Optional.empty() wrapper or a NULL_VALUE constant).
  3. If the key should simply be registered without a value, drop the put call entirely — trie nodes are created by the key walk itself.

Example fix

// before
for (Map.Entry<List<K>, V> e : entries.entrySet()) {
    trie.put(e.getKey(), e.getValue()); // NPE-prone null values
}
// after
for (Map.Entry<List<K>, V> e : entries.entrySet()) {
    if (e.getValue() != null) {
        trie.put(e.getKey(), e.getValue());
    }
}
Defensive patterns

Strategy: validation

Validate before calling

if (value == null) {
    throw new IllegalArgumentException("TrieMap does not accept null values");
}

Type guard

static <K,V> boolean isInsertable(Map.Entry<List<K>, V> e) {
    return e != null && e.getKey() != null && !e.getKey().isEmpty() && e.getValue() != null;
}

Try / catch

try {
    trie.put(key, value);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("Value cannot be null")) {
        log.warn("dropped null-valued entry for key " + key);
    } else throw e;
}

Prevention

When it happens

Trigger: trie.put(key, null) directly, or via putAll with a map containing null values; called from readEntries and the TrieMap tests.

Common situations: Bulk-loading phrase tables or maps from deserialized data that contains nulls; assembling rule maps programmatically where an absent mapping was represented as null instead of being filtered out.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/matcher/TrieMap.java:163

      return get( (Iterable<K>) key);
    } else if (key instanceof Object[]) {
      return get( Arrays.asList( (Object[]) key) );
    }
    return null;
  }

  public V get(Iterable<K> key) {
    TrieMap<K, V> curTrie = getChildTrie(key);
    return (curTrie != null) ? curTrie.value: null;
  }

  public V get(K[] key) {
    return get(Arrays.asList(key));
  }

  @Override
  public V put(Iterable<K> key, V value) {
    if (value == null) throw new IllegalArgumentException("Value cannot be null");
    TrieMap<K, V> curTrie = this;
    // go through each element
    for(K element:key){
      if (curTrie.children == null) {
        curTrie.children = new ConcurrentHashMap<>();//Generics.newConcurrentHashMap();
      }
      TrieMap<K, V> parent = curTrie;
      curTrie = curTrie.children.get(element);
      if(curTrie == null){
        parent.children.put(element, curTrie = new TrieMap<>());
      }
    }
    V oldValue = curTrie.value;
    curTrie.value = value;
    return oldValue;
  }

  public V put(K[] key, V value) {

View on GitHub (pinned to 1b7edd19c4)