{"record":{"id":"ec55b67422b29a03","repo":"stanfordnlp/CoreNLP","slug":"parser-has-not-been-loaded-and-initialized-first","errorCode":null,"errorMessage":"Parser has not been  loaded and initialized; first load a model.","messagePattern":"Parser has not been  loaded and initialized; first load a model\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/nndep/DependencyParser.java","lineNumber":977,"sourceCode":"            optTrans = tr;\n          }\n        }\n      }\n      system.apply(c, optTrans);\n    }\n    return c.tree;\n  }\n\n  /**\n   * Determine the dependency parse of the given sentence using the loaded model.\n   * You must first load a parser before calling this method.\n   *\n   * @throws java.lang.IllegalStateException If parser has not yet been loaded and initialized\n   *         (see {@link #initialize(boolean)}\n   */\n  public GrammaticalStructure predict(CoreMap sentence) {\n    if (system == null)\n      throw new IllegalStateException(\"Parser has not been  \" +\n          \"loaded and initialized; first load a model.\");\n\n    DependencyTree result = predictInner(sentence);\n\n    // The rest of this method is just busy-work to convert the\n    // package-local representation into a CoreNLP-standard\n    // GrammaticalStructure.\n\n    List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);\n    List<TypedDependency> dependencies = new ArrayList<>();\n\n    IndexedWord root = new IndexedWord(new Word(\"ROOT\"));\n    root.set(CoreAnnotations.IndexAnnotation.class, 0);\n\n    for (int i = 1; i <= result.n; i++) {\n      int head = result.getHead(i);\n      String label = result.getLabel(i);\n","sourceCodeStart":959,"sourceCodeEnd":995,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/nndep/DependencyParser.java#L959-L995","documentation":"DependencyParser.predict(CoreMap) requires the parser's internal transition `system` to be built, which happens during initialize() after loading a model. If system is null the model was never loaded/initialized, so IllegalStateException is thrown, telling the user to load a model first.","triggerScenarios":"Calling predict(sentence) or predictAnnotation on a DependencyParser instance obtained via a bare constructor (or static helpers that returned before initialize) without calling loadModel()/loadFromModelFile first.","commonSituations":"Instantiating `new DependencyParser()` for inference instead of DependencyParser.load(modelPath); forgetting to call initialize after load when calling lower-level APIs; a failed/unsuccessful model load path that left the parser half-constructed.","solutions":["Load a model before predicting: DependencyParser.load(modelPath) or parser.loadModelFile(path)","Ensure loadModel was followed by the initialization step (initialize()) if calling internal APIs","If using a pipeline, add the dependency parser annotator so it loads its model during setup"],"exampleFix":"// before\nDependencyParser parser = new DependencyParser(config);\nparser.predict(sentence); // throws\n// after\nDependencyParser parser = DependencyParser.load(\"UD_English/model.txt.gz\");\nparser.predict(sentence);","handlingStrategy":"try-catch","validationCode":"DependencyParser parser = DependencyParser.load(modelPath); // guarantees system is initialized\nparser.predict(sentence);","typeGuard":null,"tryCatchPattern":"try {\n  parser.predict(sentence);\n} catch (IllegalStateException e) {\n  parser = DependencyParser.load(modelPath); // lazy (re)load then retry\n  parser.predict(sentence);\n}","preventionTips":["Use the static DependencyParser.load()/loadFromModelFile factories for inference","Never call predict on a parser built with the raw constructor","Load models once at app startup and reuse the instance"],"tags":["java","illegal-state","nndep","model-loading"],"backgroundTag":"invalid-state-transition","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"}