{"record":{"id":"4997d253c7e504dc","repo":"apache/flink","slug":"comparatorfactory-has-not-been-initialized-from-co","errorCode":null,"errorMessage":"ComparatorFactory has not been initialized from configuration.","messagePattern":"ComparatorFactory has not been initialized from configuration\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RuntimeComparatorFactory.java","lineNumber":72,"sourceCode":"    public void readParametersFromConfig(Configuration config, ClassLoader cl)\n            throws ClassNotFoundException {\n        try {\n            comparator =\n                    (TypeComparator<T>)\n                            InstantiationUtil.readObjectFromConfig(config, CONFIG_KEY, cl);\n        } catch (ClassNotFoundException e) {\n            throw e;\n        } catch (Exception e) {\n            throw new RuntimeException(\"Could not serialize serializer into the configuration.\", e);\n        }\n    }\n\n    @Override\n    public TypeComparator<T> createComparator() {\n        if (comparator != null) {\n            return comparator;\n        } else {\n            throw new RuntimeException(\n                    \"ComparatorFactory has not been initialized from configuration.\");\n        }\n    }\n}\n","sourceCodeStart":54,"sourceCodeEnd":77,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RuntimeComparatorFactory.java#L54-L77","documentation":"RuntimeComparatorFactory.createComparator throws this when its comparator field is null, i.e. the factory was asked to produce a comparator before readParametersFromConfig successfully populated it. It is a lifecycle misuse: the factory is designed to be written to config on the client and re-initialized from config on the cluster, never used directly after bare construction.","triggerScenarios":"Constructing RuntimeComparatorFactory (no-arg or with a comparator) and calling createComparator() without a preceding successful readParametersFromConfig; or readParametersFromConfig failing early (null config/classloader throws NPE, or deserialization error) and createComparator being called anyway.","commonSituations":"Unit tests that instantiate the factory directly instead of round-tripping through Configuration. Framework code paths that reuse a factory instance after a failed config read. Copy-paste integration code that skips the write/read config round trip.","solutions":["Always run the full lifecycle: writeParametersToConfig(config) on the sender, readParametersFromConfig(config, cl) on the receiver, then createComparator().","In tests, round-trip the factory through a Configuration and InstantiationUtil to reproduce production behavior instead of calling createComparator on a fresh instance.","If readParametersFromConfig can fail, fail fast and do not call createComparator; propagate the earlier error instead of masking it with this one."],"exampleFix":"// before\nRuntimeComparatorFactory<MyType> f = new RuntimeComparatorFactory<>(myComparator);\nTypeComparator<MyType> c = f.createComparator(); // if comparator was never set/read -> boom\n\n// after\nConfiguration cfg = new Configuration();\nnew RuntimeComparatorFactory<>(myComparator).writeParametersToConfig(cfg);\nRuntimeComparatorFactory<MyType> f2 = new RuntimeComparatorFactory<>();\nf2.readParametersFromConfig(cfg, getClass().getClassLoader());\nTypeComparator<MyType> c = f2.createComparator();","handlingStrategy":"validation","validationCode":"public static RuntimeComparatorFactory<T> roundTrip<T>(\n        RuntimeComparatorFactory<T> src, Configuration cfg, ClassLoader cl)\n        throws ClassNotFoundException {\n    src.writeParametersToConfig(cfg);\n    RuntimeComparatorFactory<T> out = new RuntimeComparatorFactory<>();\n    out.readParametersFromConfig(cfg, cl);\n    return out; // only call createComparator() on the returned, initialized factory\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat ComparatorFactory as write->read->create lifecycle; never call createComparator on an uninitialized instance.","Abort the operator setup if readParametersFromConfig throws; do not continue to createComparator.","In tests, always round-trip through a Configuration instead of direct construction."],"tags":["lifecycle","comparator","initialization","misuse"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}