{"record":{"id":"6a3547fa77af5e03","repo":"apache/flink","slug":"cannot-create-accumulator-accumulatorclass-getna","errorCode":null,"errorMessage":"Cannot create accumulator ${accumulatorClass.getName()}","messagePattern":"Cannot create accumulator (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/functions/util/AbstractRuntimeUDFContext.java","lineNumber":191,"sourceCode":"    }\n\n    // --------------------------------------------------------------------------------------------\n\n    @SuppressWarnings(\"unchecked\")\n    private <V, A extends Serializable> Accumulator<V, A> getAccumulator(\n            String name, Class<? extends Accumulator<V, A>> accumulatorClass) {\n\n        Accumulator<?, ?> accumulator = accumulators.get(name);\n\n        if (accumulator != null) {\n            AccumulatorHelper.compareAccumulatorTypes(\n                    name, accumulator.getClass(), accumulatorClass);\n        } else {\n            // Create new accumulator\n            try {\n                accumulator = accumulatorClass.newInstance();\n            } catch (Exception e) {\n                throw new RuntimeException(\n                        \"Cannot create accumulator \" + accumulatorClass.getName());\n            }\n            accumulators.put(name, accumulator);\n        }\n        return (Accumulator<V, A>) accumulator;\n    }\n\n    @Override\n    @PublicEvolving\n    public <T> ValueState<T> getState(ValueStateDescriptor<T> stateProperties) {\n        throw new UnsupportedOperationException(\n                \"This state is only accessible by functions executed on a KeyedStream\");\n    }\n\n    @Override\n    @PublicEvolving\n    public <T> ListState<T> getListState(ListStateDescriptor<T> stateProperties) {\n        throw new UnsupportedOperationException(","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/functions/util/AbstractRuntimeUDFContext.java#L173-L209","documentation":"Thrown by AbstractRuntimeUDFContext.getAccumulator(name, accumulatorClass) when the accumulatorClass cannot be instantiated via reflection (accumulatorClass.newInstance()). The most common cause is that the accumulator class lacks a public no-arg constructor, is abstract, or is an interface. The RuntimeException swallows the original exception (InstantiationException or IllegalAccessException).","triggerScenarios":"A RichFunction calls getRuntimeContext().getAccumulator(name, accumulatorClass) with a class that cannot be reflectively instantiated — no public no-arg constructor, abstract class, interface, or a constructor that throws.","commonSituations":"Custom Accumulator subclass with only a parameterized constructor. Passing an interface type or abstract base class instead of a concrete implementation. Accumulator class whose constructor throws an exception during instance creation.","solutions":["Ensure your custom Accumulator class has a public no-arg constructor.","If the accumulator needs parameters, use addAccumulator() to pre-construct and register it in open(), then retrieve it with getAccumulator(name) instead of passing the class.","Verify you are passing a concrete class, not an interface or abstract type."],"exampleFix":"// before — class has no no-arg constructor\npublic class MyCounter implements Accumulator<Long, Long> {\n    private final String label;\n    public MyCounter(String label) { this.label = label; }\n}\ngetRuntimeContext().getAccumulator(\"c\", MyCounter.class); // throws\n\n// after — add a no-arg constructor and pre-register\npublic class MyCounter implements Accumulator<Long, Long> {\n    private String label = \"default\";\n    public MyCounter() {}\n    public MyCounter(String label) { this.label = label; }\n}\n// in open():\ngetRuntimeContext().addAccumulator(\"c\", new MyCounter(\"custom\"));","handlingStrategy":"validation","validationCode":"// Verify the class has a public no-arg constructor before passing it\ntry {\n    accumulatorClass.getDeclaredConstructor();\n} catch (NoSuchMethodException e) {\n    throw new IllegalArgumentException(\"Accumulator class must have a public no-arg constructor\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure custom Accumulator classes have a public no-arg constructor.","Prefer addAccumulator() with a pre-built instance over getAccumulator(name, class) when the class needs parameters.","Use concrete classes, not interfaces or abstract types, as the accumulatorClass argument."],"tags":["accumulator","reflection","runtime-context","user-error"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}