stanfordnlp/CoreNLP · error · NullPointerException

Attempted to parse empty/null tag

Error message

Attempted to parse empty/null tag

What it means

XMLUtils.XMLTag's constructor throws NullPointerException when given a null or empty string, since an XML tag must contain at least '<' and '>'. The unusual choice of NPE signals 'missing required input' rather than malformed content.

Solutions

  1. Check the string is non-null and non-empty before constructing the XMLTag.
  2. Fix upstream extraction so only real tag substrings (matching <...>) are passed.
  3. Catch NullPointerException and skip/log the malformed fragment.

Example fix

// before
xmlUtils.addTag(new XMLTag(matcher.group(1)));
// after
String g = matcher.group(1);
if (g != null && !g.isEmpty()) xmlUtils.addTag(new XMLTag(g));
Defensive patterns

Strategy: type-guard

Validate before calling

if (tag == null || tag.isEmpty()) skip();

Type guard

static boolean isPlausibleTag(String s) {
  return s != null && s.length() >= 2 && s.charAt(0) == '<' && s.charAt(s.length()-1) == '>' && !s.equals("<>");
}
// use: if (isPlausibleTag(candidate)) add(new XMLTag(candidate));

Try / catch

try {
  new XMLTag(candidate);
} catch (NullPointerException e) {
  log.fine("skipping empty tag token");
}

Prevention

When it happens

Trigger: new XMLTag("") or new XMLTag(null), typically when a tag-extraction regex/substring produced nothing (e.g. matcher.group() on a non-matching region) and the result was passed straight in.

Common situations: Streaming XML tokenizers that split text into tags and feed each token to XMLTag; input documents with stray or truncated markup yielding empty candidate strings.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/util/XMLUtils.java:1091

    public String name;

    /** Stores attributes as a Map from keys to values. */
    public Map<String,String> attributes;

    /** Whether this is an ending tag or not. */
    public boolean isEndTag;

    /** Whether this is an empty element expressed as a single empty element tag like {@code <p/>}. */
    public boolean isSingleTag;

    /**
     * Assumes that String contains an XML tag.
     *
     * @param tag String to turn into an XMLTag object
     */
    public XMLTag(String tag) {
      if (tag == null || tag.isEmpty()) {
        throw new NullPointerException("Attempted to parse empty/null tag");
      }
      if (tag.charAt(0) != '<') {
        throw new IllegalArgumentException("Tag did not start with <");
      }
      if (tag.charAt(tag.length() - 1) != '>') {
        throw new IllegalArgumentException("Tag did not end with >");
      }
      text = tag;
      int begin = 1;
      if (tag.charAt(1) == '/') {
        begin = 2;
        isEndTag = true;
      } else {
        isEndTag = false;
      }
      int end = tag.length() - 1;
      if (tag.charAt(tag.length() - 2) == '/') {
        end = tag.length() - 2;

View on GitHub (pinned to 1b7edd19c4)