{"record":{"id":"9ebf6abc3a9ac765","repo":"stanfordnlp/CoreNLP","slug":"error-occurred-while-constructing-treereaderfactor","errorCode":null,"errorMessage":"Error occurred while constructing TreeReaderFactory: \" + e","messagePattern":"Error occurred while constructing TreeReaderFactory: \" \\+ e","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/tregex/TregexPattern.java","lineNumber":828,"sourceCode":"      }\n      if (TRegexTreeVisitor.printNumMatchesToStdOut) {\n        System.out.println(vis.numMatches());\n      }\n    } catch (IOException e) {\n      log.warn(e);\n    } catch (TregexParseException e) {\n      errPW.println(\"Error parsing expression: \" + args[0]);\n      errPW.println(\"Parse exception: \" + e);\n    }\n  }\n\n  private static TreeReaderFactory getTreeReaderFactory(String treeReaderFactoryClassName) {\n    TreeReaderFactory trf = new TRegexTreeReaderFactory();\n    if (treeReaderFactoryClassName != null) {\n      try {\n        trf = (TreeReaderFactory) Class.forName(treeReaderFactoryClassName).getDeclaredConstructor().newInstance();\n      } catch (Exception e) {\n        throw new RuntimeException(\"Error occurred while constructing TreeReaderFactory: \" + e);\n      }\n    }\n    return trf;\n  }\n\n  private static Treebank treebank; // used by main method, must be accessible\n\n  private static final long serialVersionUID = 5060298043763944913L;\n\n\n  // not thread-safe, but only used by TregexPattern's main method\n  private static class TRegexTreeVisitor implements TreeVisitor {\n\n    private static boolean printNumMatchesToStdOut = false;\n    static boolean printNonMatchingTrees = false;\n    static boolean printSubtreeCode = false;\n    static boolean printTree = false;\n    static boolean printWholeTreeOnly = false;","sourceCodeStart":810,"sourceCodeEnd":846,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/tregex/TregexPattern.java#L810-L846","documentation":"TregexPattern.getTreeReaderFactory loads a custom TreeReaderFactory class by reflection from the -treeReaderFactory command-line option. If the class name cannot be found, instantiated, or cast to TreeReaderFactory, the raw reflective exception is wrapped in a RuntimeException and rethrown.","triggerScenarios":"Running the TregexPattern main() with -treeReaderFactory <className> where className is misspelled, not on the classpath, lacks a no-arg constructor, its constructor throws, or it does not implement TreeReaderFactory (ClassCastException).","commonSituations":"Typo in the fully-qualified class name; jar containing the custom factory not on the runtime classpath; factory class was refactored/renamed across versions; constructor requires arguments or throws during initialization.","solutions":["Verify the fully-qualified class name passed to -treeReaderFactory is spelled correctly and the class implements edu.stanford.nlp.trees.TreeReaderFactory.","Ensure the jar/class file is on the runtime classpath (check with `java -cp ...` and try loading the class via Class.forName manually).","Confirm the class has a public no-argument constructor; add one if missing.","Inspect the nested exception `e` in the message (and its stack trace) to see the actual reflective failure cause."],"exampleFix":"// before\njava edu.stanford.nlp.trees.tregex.TregexPattern -treeReaderFactory MyFact treeFile\n// after (correct, on-classpath class implementing TreeReaderFactory)\njava -cp .:stanford-corenlp.jar edu.stanford.nlp.trees.tregex.TregexPattern -treeReaderFactory com.example.MyTreeReaderFactory treeFile","handlingStrategy":"validation","validationCode":"try {\n  Class<?> c = Class.forName(factoryClassName);\n  if (!TreeReaderFactory.class.isAssignableFrom(c)) throw new IllegalArgumentException(factoryClassName + \" is not a TreeReaderFactory\");\n  c.getDeclaredConstructor().newInstance();\n} catch (Exception e) {\n  throw new IllegalArgumentException(\"Bad -treeReaderFactory class: \" + factoryClassName, e);\n}","typeGuard":"static boolean isValidTreeReaderFactory(String name) {\n  try {\n    return Class.forName(name) != null && TreeReaderFactory.class.isAssignableFrom(Class.forName(name));\n  } catch (Exception e) { return false; }\n}","tryCatchPattern":"try {\n  Treebank tb = ...; // runs with the custom factory\n} catch (RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"constructing TreeReaderFactory\")) {\n    System.err.println(\"Fix -treeReaderFactory option: \" + e.getCause());\n  } else throw e;\n}","preventionTips":["Always pass the fully-qualified class name and keep the factory class on the runtime classpath.","Keep a public no-arg constructor in custom TreeReaderFactory implementations.","Smoke-test the factory class with a tiny treebank before long batch runs."],"tags":["reflection","classpath","configuration"],"backgroundTag":"class-not-found","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"}