{"record":{"id":"0e86d80020825b2f","repo":"karatelabs/karate","slug":"is-not-a-constructor","errorCode":null,"errorMessage":" is not a constructor","messagePattern":" is not a constructor","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JavaUtils.java","lineNumber":52,"sourceCode":"        if (List.class.isAssignableFrom(clazz)) return \"Array\";\n        if (clazz.isArray()) return \"Array\";\n        if (clazz == String.class) return \"String\";\n        if (Number.class.isAssignableFrom(clazz) || clazz == int.class\n                || clazz == double.class || clazz == long.class\n                || clazz == float.class || clazz == short.class\n                || clazz == byte.class) return \"Number\";\n        if (clazz == Boolean.class || clazz == boolean.class) return \"Boolean\";\n        if (clazz == Character.class || clazz == char.class) return \"String\";\n        if (Set.class.isAssignableFrom(clazz)) return \"Set\";\n        return clazz.getSimpleName();\n    }\n\n    static Object construct(Class<?> clazz, Object[] args) {\n        try {\n            Constructor<?> constructor = findConstructor(clazz, args);\n            return constructor.newInstance(args);\n        } catch (Exception e) {\n            throw JsErrorException.typeError(jsTypeName(clazz) + \" is not a constructor\");\n        }\n    }\n\n    static Object invokeStatic(Class<?> clazz, String name, Object[] args) {\n        Method method = findMethod(clazz, name, args);\n        if (method == null) {\n            throw JsErrorException.typeError(\".\" + name + \" is not a function (called on \" + jsTypeName(clazz) + \")\");\n        }\n        try {\n            return invoke(null, method, args);\n        } catch (InvocationTargetException e) {\n            Throwable cause = e.getCause();\n            if (cause instanceof RuntimeException re) {\n                throw re;\n            }\n            if (cause instanceof Error err) {\n                throw err;\n            }","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JavaUtils.java#L34-L70","documentation":"Karate's JS engine throws this when JS code tries to use Java interop (`new Java.type('...')(...)` style construction) but no matching constructor can be found on the resolved Java class. JavaUtils.construct calls findConstructor; if none matches the supplied arguments (or the class has no public constructor), it wraps the failure as a JS TypeError '<TypeName> is not a constructor'.","triggerScenarios":"Calling `new` on a Java class handle with argument types/counts that match no declared constructor, or on a class/inner class that is abstract, an interface, or has only non-public constructors.","commonSituations":"Typo in the fully-qualified class name resolving to the wrong type; passing JS strings/numbers that don't convert to the expected Java parameter types; trying to instantiate abstract classes like java.util.Map or nested classes addressed incorrectly.","solutions":["Check the Java class actually has a public constructor matching the number and types of the arguments you pass.","For abstract classes/interfaces, use a factory method instead, e.g. java.util.Map.of(...) or Java.type('java.util.HashMap').","Convert JS values explicitly (e.g. use java.lang.Integer.valueOf) when overload resolution fails.","Verify nested class access uses the correct syntax (Java.type('outer.inner') rather than property access on the outer type)."],"exampleFix":"// before\nvar List = Java.type('java.util.List');\nvar l = new List();\n// after\nvar ArrayList = Java.type('java.util.ArrayList');\nvar l = new ArrayList();","handlingStrategy":"try-catch","validationCode":"var Clz = Java.type('java.util.HashMap');\n// confirm a public constructor exists by attempting a no-arg build in setup, not at test time","typeGuard":"function isConstructible(clazz) { try { return typeof clazz === 'object' && clazz.getClass && !java.lang.reflect.Modifier.isAbstract(clazz.getClass().getModifiers()); } catch (e) { return false; } }","tryCatchPattern":"try { var obj = new Clz(); } catch (e) { if (String(e).indexOf('is not a constructor') !== -1) { obj = Clz.getDefault ? Clz.getDefault() : null; } else { throw e; } }","preventionTips":["Only use `new` on concrete classes with public constructors","Prefer factory methods (Map.of, List.of) for interfaces and abstract types","Match argument types to the declared constructor signature","Pin the JDK/library version and check its javadoc before interop calls"],"tags":["javascript","java-interop","constructor","reflection"],"backgroundTag":"js-not-a-constructor","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}