{"record":{"id":"afdc39d549b6f5ed","repo":"apache/pulsar","slug":"class-constructor-throws-exception","errorCode":null,"errorMessage":"Class  constructor throws exception","messagePattern":"Class  constructor throws exception","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"pulsar-functions/runtime-all/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceMain.java","lineNumber":152,"sourceCode":"        try {\n            theCls = Class.forName(userClassName, true, classLoader);\n        } catch (ClassNotFoundException | NoClassDefFoundError cnfe) {\n            throw new RuntimeException(\"Class \" + userClassName + \" must be in class path\", cnfe);\n        }\n        Object result;\n        try {\n            Constructor<?> meth = theCls.getDeclaredConstructor();\n            meth.setAccessible(true);\n\n            result = meth.newInstance();\n        } catch (InstantiationException ie) {\n            throw new RuntimeException(\"User class must be concrete\", ie);\n        } catch (NoSuchMethodException e) {\n            throw new RuntimeException(\"Class \" + userClassName + \" doesn't have such method\", e);\n        } catch (IllegalAccessException e) {\n            throw new RuntimeException(\"Class \" + userClassName + \" must have a no-arg constructor\", e);\n        } catch (InvocationTargetException e) {\n            throw new RuntimeException(\"Class \" + userClassName + \" constructor throws exception\", e);\n        }\n        return result;\n    }\n\n    public static ClassLoader loadJar(ClassLoader parent, File[] jars) throws MalformedURLException {\n        URL[] urls = new URL[jars.length];\n        for (int i = 0; i < jars.length; i++) {\n            urls[i] = jars[i].toURI().toURL();\n        }\n        return new URLClassLoader(urls, parent);\n    }\n\n    public static boolean isBlank(String str) {\n        int strLen;\n        if (str != null && (strLen = str.length()) != 0) {\n            for (int i = 0; i < strLen; ++i) {\n                if (!Character.isWhitespace(str.charAt(i))) {\n                    return false;","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-functions/runtime-all/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceMain.java#L134-L170","documentation":"createInstance() invokes the no-arg constructor via reflection; InvocationTargetException means the constructor body itself threw an exception, which Pulsar rethrows as RuntimeException(\"Class X constructor throws exception\") with the original cause attached via getCause().","triggerScenarios":"The user function's no-arg constructor performs initialization that fails: reading a missing file/env var, connecting to an external service, throwing from static/instance initializers, or failing NPE in field initialization.","commonSituations":"Heavy work in constructors (connecting to DBs, reading configs) instead of open(); static initializer throwing; dependencies loaded in the constructor that aren't on the classpath ( NoClassDefFoundError wrapped as InvocationTargetException).","solutions":["Inspect the cause chain (e.printStackTrace / logs) — the real error is inside InvocationTargetException.getCause().","Move initialization out of the constructor into open()/initialize(Context) where failures are handled by the instance lifecycle.","Make the constructor trivially safe; defer resource acquisition and config reading.","Ensure any classes touched by the constructor are present in the function jar/classpath."],"exampleFix":"// before\npublic MyFunction() { conn = DriverManager.getConnection(url); }\n// after\npublic MyFunction() {}\npublic void open(Map config, FunctionContext ctx) { conn = DriverManager.getConnection(url); }","handlingStrategy":"try-catch","validationCode":"// dry-run the constructor before submitting\nClass<?> c = Class.forName(className);\nc.getDeclaredConstructor().newInstance();","typeGuard":"null","tryCatchPattern":"try { Object f = JavaInstanceMain.createInstance(className, cl); } catch (RuntimeException e) { if (e.getMessage().contains(\"constructor throws exception\")) { e.getCause().printStackTrace(); /* the real error */ } throw e; }","preventionTips":["Keep constructors empty; initialize in open()/initialize(Context).","Never do I/O (DB, network, files) in function constructors.","Dry-run instantiation in your function's unit tests.","Check InvocationTargetException.getCause() first when debugging."],"tags":["reflection","constructor","initialization","function-config"],"backgroundTag":"constructor-threw-exception","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"}