stanfordnlp/CoreNLP · warning
Failed to handle | |
Error message
Failed to handle |${s}| What it means
readAndParseTag constructs an XMLTag from the given string; any Exception thrown by the XMLTag constructor is caught and logged as 'Failed to handle |s|', and the method returns null. The |s| in the message is the offending input string. This means the argument was not a recognizable XML tag, and callers who do not null-check will hit a NullPointerException downstream.
Solutions
- Null-check the return value of readAndParseTag before use
- Validate that the input starts with '<' and ends with '>' and contains a tag name before calling
- Read the logged warning to see which string failed and correct its construction
- Wrap the call in your own try/catch or Optional handling at the call site
Example fix
// before
XMLTag t = XMLUtils.readAndParseTag(s);
String name = t.name; // NPE when s is not a valid tag
// after
XMLTag t = XMLUtils.readAndParseTag(s);
if (t == null) { throw new IllegalArgumentException("Not a valid XML tag: " + s); }
String name = t.name; Defensive patterns
Strategy: type-guard
Validate before calling
if (s == null || !(s.startsWith("<") && s.endsWith(">") && s.length() > 2))
throw new IllegalArgumentException("Not an XML tag: " + s); Type guard
XMLTag t = XMLUtils.readAndParseTag(s);
if (t == null) throw new IllegalArgumentException("readAndParseTag could not parse: " + s); Try / catch
XMLTag t = XMLUtils.readAndParseTag(s);
if (t == null) {
throw new IllegalArgumentException("Invalid XML tag input: " + s);
} Prevention
- Always null-check the result — it returns null on any parse failure
- Ensure sliced tag strings retain their delimiters
- Check logs for 'Failed to handle |...|' to find malformed inputs
- Validate tag syntax before calling
When it happens
Trigger: Calling XMLUtils.readAndParseTag(s) (also reached via XMLUtils.tag(...)) with a string that XMLTag cannot parse — missing angle brackets, malformed attributes, empty/null-like input, or text that is not a single tag.
Common situations: Splitting documents into tokens and feeding non-tag text fragments; hand-built tag strings with unescaped quotes or stray characters; off-by-one slicing that cuts off the closing '>' ; assuming the return is never null.
Related errors
- Attempted to parse empty/null tag
- attempt to get word when sentence and lattice are null!
- org.xml.sax.SAXException
- TIMEX3 should only contain text
- unexpected element
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/a5b9da3440870c8f.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/util/XMLUtils.java:418
b.append((char) c);
c = r.read();
}
return b.toString();
}
/**
* @return the new XMLTag object, or null if couldn't be created
*/
public static XMLTag readAndParseTag(Reader r) throws IOException {
String s = readTag(r);
if (s == null) {
return null;
}
XMLTag ret = null;
try {
ret = new XMLTag(s);
} catch (Exception e) {
log.warn("Failed to handle |" + s + "|");
}
return ret;
}
// Pattern is reentrant, going by the statement "many matchers can share the same pattern"
// on the Pattern javadoc. Therefore, this should be safe as a static final variable.
private static final Pattern xmlEscapingPattern = Pattern.compile("&.+?;");
public static String unescapeStringForXML(String s) {
StringBuilder result = new StringBuilder();
Matcher m = xmlEscapingPattern.matcher(s);
int end = 0;
while (m.find()) {
int start = m.start();
result.append(s, end, start);
end = m.end();
result.append(translate(s.substring(start, end)));
}View on GitHub (pinned to 1b7edd19c4)