{"record":{"id":"8a0092fb0ee9aee8","repo":"stanfordnlp/CoreNLP","slug":"can-t-parse-a-zero-length-sentence","errorCode":null,"errorMessage":"Can't parse a zero-length sentence!","messagePattern":"Can't parse a zero-length sentence!","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/lexparser/LexicalizedParserQuery.java","lineNumber":214,"sourceCode":"   * @param sentence The sentence to parse\n   * @return true Iff the sentence was accepted by the grammar\n   * @throws UnsupportedOperationException If the Sentence is too long or\n   *                                       of zero length or the parse\n   *                                       otherwise fails for resource reasons\n   */\n  private boolean parseInternal(List<? extends HasWord> sentence) {\n    parseSucceeded = false;\n    parseNoMemory = false;\n    parseUnparsable = false;\n    parseSkipped = false;\n    parseFallback = false;\n    whatFailed = null;\n    addedPunct = false;\n    originalSentence = sentence;\n    int length = sentence.size();\n    if (length == 0) {\n      parseSkipped = true;\n      throw new UnsupportedOperationException(\"Can't parse a zero-length sentence!\");\n    }\n\n    List<HasWord> sentenceB;\n    if (op.wordFunction != null) {\n      sentenceB = Generics.newArrayList();\n      for (HasWord word : originalSentence) {\n        if (word instanceof Label) {\n          Label label = (Label) word;\n          Label newLabel = label.labelFactory().newLabel(label);\n          if (newLabel instanceof HasWord) {\n            sentenceB.add((HasWord) newLabel);\n          } else {\n            throw new AssertionError(\"This should have been a HasWord\");\n          }\n        } else if (word instanceof HasTag) {\n          TaggedWord tw = new TaggedWord(word.word(), ((HasTag) word).tag());\n          sentenceB.add(tw);\n        } else {","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/lexparser/LexicalizedParserQuery.java#L196-L232","documentation":"LexicalizedParserQuery.parseInternal rejects an input sentence with zero tokens by setting parseSkipped and throwing UnsupportedOperationException(\"Can't parse a zero-length sentence!\"). A constituency parse requires at least one token, so an empty input can never produce a tree.","triggerScenarios":"Calling parser.parse(new ArrayList<>()) / parserQuery.parse(Collections.emptyList()) or feeding empty text through a tokenizer that yields no words.","commonSituations":"Passing whitespace-only or punctuation-stripped text through the tokenizer; processing blank lines in a batch loop; upstream sentence splitting producing empty segments; accidentally clearing the word list before parsing.","solutions":["Check sentence.size() > 0 before calling parse and skip/handle empty inputs yourself","Guard the text before tokenization: trim and test for empty/whitespace-only strings","In batch pipelines, filter out empty sentences produced by the sentence splitter","Catch UnsupportedOperationException around parse if empty input is expected and recoverable"],"exampleFix":"// before\nparser.parse(sentence); // sentence may be empty\n// after\nif (!sentence.isEmpty()) {\n  parser.parse(sentence);\n}","handlingStrategy":"validation","validationCode":"// Skip empty inputs before parsing\nif (sentence == null || sentence.isEmpty()) return null; // or log & skip\n","typeGuard":null,"tryCatchPattern":"try {\n  Tree t = parser.parse(sentence);\n} catch (UnsupportedOperationException e) {\n  if (e.getMessage().contains(\"zero-length\")) return null;\n  throw e;\n}","preventionTips":["Filter blank/whitespace-only lines before tokenization","Check tokenized output size before parse, not just raw text length","Drop empty segments produced by sentence splitters in batch pipelines"],"tags":["parser","empty-input","validation","java"],"backgroundTag":"empty-required-field","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"}