{"record":{"id":"f229b49f8a8aa463","repo":"stanfordnlp/CoreNLP","slug":"no-roots-in-graph-this-find-where-this-grap","errorCode":null,"errorMessage":"No roots in graph:\n + this + \nFind where this graph was created and make sure you're adding roots.","messagePattern":"No roots in graph:\n \\+ this \\+ \nFind where this graph was created and make sure you're adding roots\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/semgraph/SemanticGraph.java","lineNumber":832,"sourceCode":"   *\n   * @return A list of root nodes or an empty list.\n   */\n  private List<IndexedWord> getVerticesWithoutParents() {\n    List<IndexedWord> result = new ArrayList<>();\n    for (IndexedWord v : vertexSet()) {\n      int inDegree = inDegree(v);\n      if (inDegree == 0) {\n        result.add(v);\n      }\n    }\n    Collections.sort(result);\n    return result;\n  }\n\n  /** Returns the (first) root of this SemanticGraph. */\n  public IndexedWord getFirstRoot() {\n    if (roots.isEmpty())\n      throw new RuntimeException(\"No roots in graph:\\n\" + this\n          + \"\\nFind where this graph was created and make sure you're adding roots.\");\n    return roots.iterator().next();\n  }\n\n  public void addRoot(IndexedWord root) {\n    addVertex(root);\n    roots.add(root);\n  }\n\n  /**\n   * This method should not be used if possible. TODO: delete it\n   *\n   * Recomputes the roots, based of actual candidates. This is done to\n   * ensure a rooted tree after a sequence of edits. If the none of the vertices\n   * can act as a root (due to a cycle), keep old rootset, retaining only the\n   * existing vertices on that list.\n   *\n   * TODO: this cannot deal with \"Hamburg is a city which everyone likes\", as","sourceCodeStart":814,"sourceCodeEnd":850,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/semgraph/SemanticGraph.java#L814-L850","documentation":"getFirstRoot() throws a RuntimeException when the graph has no root vertices at all. A SemanticGraph is expected to always have at least one root; an empty root set means the graph was built incorrectly (vertices added without calling addRoot or from an empty/degenerate parse).","triggerScenarios":"Calling getFirstRoot() on a SemanticGraph constructed manually with addVertex() but no addRoot(), on a graph deserialized from an empty annotation, or on a graph whose roots were never populated by the producing parser.","commonSituations":"Parsing empty or whitespace-only sentences; using Semgrex or dependency conversion pipelines that produce empty graphs; building SemanticGraphs programmatically and forgetting addRoot; sentence filtered so heavily that all roots were removed.","solutions":["Ensure every graph-producing path calls addRoot for the root vertex (or uses a builder that does).","Guard the call: check graph.isEmpty() / graph.getRoots().isEmpty() before getFirstRoot().","If the sentence may be empty, skip processing rather than forcing a root lookup."],"exampleFix":"// before\nIndexedWord root = sg.getFirstRoot();\n// after\nif (sg.getRoots().isEmpty()) {\n  return; // or handle empty graph\n}\nIndexedWord root = sg.getFirstRoot();","handlingStrategy":"validation","validationCode":"if (sg == null || sg.isEmpty() || sg.getRoots().isEmpty()) { /* skip sentence */ return; }","typeGuard":null,"tryCatchPattern":"try { IndexedWord root = sg.getFirstRoot(); } catch (RuntimeException e) { log.warn(\"graph without roots skipped\"); }","preventionTips":["Skip empty or whitespace-only sentences before annotating.","Call addRoot (or use a builder/parser pipeline) whenever constructing SemanticGraphs manually.","Assert non-empty roots right after graph construction in tests."],"tags":["nlp","dependency-graph","empty-graph"],"backgroundTag":"empty-result-set","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}