{"record":{"id":"f371fe99bf4ac79c","repo":"apache/pulsar","slug":"user-class-constructor-throws-exception-f371fe","errorCode":null,"errorMessage":"User class constructor throws exception","messagePattern":"User class constructor throws exception","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/windowing/WindowFunctionExecutor.java","lineNumber":154,"sourceCode":"        } catch (ClassNotFoundException | NoClassDefFoundError cnfe) {\n            throw new RuntimeException(\n                    String.format(\"Timestamp extractor class %s must be in class path\",\n                            windowConfig.getTimestampExtractorClassName()), cnfe);\n        }\n\n        Object result;\n        try {\n            Constructor<?> constructor = theCls.getDeclaredConstructor();\n            constructor.setAccessible(true);\n            result = constructor.newInstance();\n        } catch (InstantiationException ie) {\n            throw new RuntimeException(\"User class must be concrete\", ie);\n        } catch (NoSuchMethodException e) {\n            throw new RuntimeException(\"User class doesn't have such method\", e);\n        } catch (IllegalAccessException e) {\n            throw new RuntimeException(\"User class must have a no-arg constructor\", e);\n        } catch (InvocationTargetException e) {\n            throw new RuntimeException(\"User class constructor throws exception\", e);\n        }\n        Class<?>[] timestampExtractorTypeArgs = TypeResolver.resolveRawArguments(\n                TimestampExtractor.class, result.getClass());\n        Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Function.class, this.getClass());\n        if (!typeArgs[0].equals(timestampExtractorTypeArgs[0])) {\n            throw new RuntimeException(\n                    \"Inconsistent types found between function input type and timestamp extractor type: \"\n                            + \" function type = \" + typeArgs[0] + \", timestamp extractor type = \"\n                            + timestampExtractorTypeArgs[0]);\n        }\n        return (TimestampExtractor<T>) result;\n    }\n\n    private TriggerPolicy<Record<T>, ?> getTriggerPolicy(WindowConfig windowConfig, WindowManager<Record<T>> manager,\n                                                         EvictionPolicy<Record<T>, ?> evictionPolicy, Context context) {\n        if (windowConfig.getSlidingIntervalCount() != null) {\n            if (this.isEventTime()) {\n                return new WatermarkCountTriggerPolicy<>(","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/windowing/WindowFunctionExecutor.java#L136-L172","documentation":"If the extractor's no-arg constructor itself throws during newInstance(), the InvocationTargetException is rethrown as \"User class constructor throws exception\", with the original cause chained. This means the extractor class was found, concrete, and accessible, but its own initialization code failed at runtime. Inspect the chained cause for the real failure.","triggerScenarios":"Constructor performs I/O, loads config/resources, connects to a service, or dereferences null and throws; reflective invocation wraps it in InvocationTargetException during initialize/getWindowManager.","commonSituations":"Constructor reading a missing config file or env var; static initializers failing; environment-specific setup (paths, credentials) not available inside the function instance.","solutions":["Read the caused-by of this exception in the function logs to find the constructor's real failure","Move heavy/failing initialization out of the constructor into lazy first use or open()/close lifecycle hooks","Harden the constructor against missing config (defaults, null checks)","Rebuild/redeploy and re-test with the environment the function instance actually runs in"],"exampleFix":"// before\npublic MyExtractor() {\n    this.settings = Files.readAllBytes(Paths.get(\"/etc/extractor.conf\")); // throws in function runtime\n}\n// after\npublic MyExtractor() {\n    this.settings = loadOrDefault(\"/etc/extractor.conf\");\n}\nprivate static byte[] loadOrDefault(String p) {\n    try { return Files.readAllBytes(Paths.get(p)); } catch (Exception e) { return new byte[0]; }\n}","handlingStrategy":"try-catch","validationCode":"// verify the constructor runs cleanly in the same environment as the function\ntry {\n    Class.forName(className).getDeclaredConstructor().newInstance();\n} catch (InvocationTargetException e) {\n    throw new IllegalStateException(\"extractor ctor throws: \" + e.getTargetException(), e);\n}","typeGuard":null,"tryCatchPattern":"try {\n    executor.initialize();\n} catch (RuntimeException e) {\n    if (\"User class constructor throws exception\".equals(e.getMessage()) && e.getCause() != null) {\n        // e.getCause() is the real exception thrown by the extractor constructor\n        throw new IllegalStateException(\"extractor init failed: \" + e.getCause().getMessage(), e.getCause());\n    }\n    throw e;\n}","preventionTips":["Keep extractor constructors trivial; defer I/O to first use","Never rely on local files/env vars unavailable in the function runtime","Always inspect the chained cause in function instance logs"],"tags":["pulsar-functions","windowing","reflection","constructor-exception"],"backgroundTag":"class-instantiation-failed","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}