{"record":{"id":"4849f639b7b80ea7","repo":"stanfordnlp/CoreNLP","slug":"attempted-to-parse-empty-null-tag","errorCode":null,"errorMessage":"Attempted to parse empty/null tag","messagePattern":"Attempted to parse empty/null tag","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/XMLUtils.java","lineNumber":1091,"sourceCode":"    public String name;\n\n    /** Stores attributes as a Map from keys to values. */\n    public Map<String,String> attributes;\n\n    /** Whether this is an ending tag or not. */\n    public boolean isEndTag;\n\n    /** Whether this is an empty element expressed as a single empty element tag like {@code <p/>}. */\n    public boolean isSingleTag;\n\n    /**\n     * Assumes that String contains an XML tag.\n     *\n     * @param tag String to turn into an XMLTag object\n     */\n    public XMLTag(String tag) {\n      if (tag == null || tag.isEmpty()) {\n        throw new NullPointerException(\"Attempted to parse empty/null tag\");\n      }\n      if (tag.charAt(0) != '<') {\n        throw new IllegalArgumentException(\"Tag did not start with <\");\n      }\n      if (tag.charAt(tag.length() - 1) != '>') {\n        throw new IllegalArgumentException(\"Tag did not end with >\");\n      }\n      text = tag;\n      int begin = 1;\n      if (tag.charAt(1) == '/') {\n        begin = 2;\n        isEndTag = true;\n      } else {\n        isEndTag = false;\n      }\n      int end = tag.length() - 1;\n      if (tag.charAt(tag.length() - 2) == '/') {\n        end = tag.length() - 2;","sourceCodeStart":1073,"sourceCodeEnd":1109,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/XMLUtils.java#L1073-L1109","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the string is non-null and non-empty before constructing the XMLTag.","Fix upstream extraction so only real tag substrings (matching <...>) are passed.","Catch NullPointerException and skip/log the malformed fragment."],"exampleFix":"// before\nxmlUtils.addTag(new XMLTag(matcher.group(1)));\n// after\nString g = matcher.group(1);\nif (g != null && !g.isEmpty()) xmlUtils.addTag(new XMLTag(g));","handlingStrategy":"type-guard","validationCode":"if (tag == null || tag.isEmpty()) skip();","typeGuard":"static boolean isPlausibleTag(String s) {\n  return s != null && s.length() >= 2 && s.charAt(0) == '<' && s.charAt(s.length()-1) == '>' && !s.equals(\"<>\");\n}\n// use: if (isPlausibleTag(candidate)) add(new XMLTag(candidate));","tryCatchPattern":"try {\n  new XMLTag(candidate);\n} catch (NullPointerException e) {\n  log.fine(\"skipping empty tag token\");\n}","preventionTips":["Never pass regex group results without a match check.","Filter empty tokens before constructing XMLTag.","Ensure tag extraction only emits bracketed substrings."],"tags":["java","xml","null","parsing"],"backgroundTag":"null-argument","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}