{"record":{"id":"8c8782b3e34912eb","repo":"MyCATApache/Mycat-Server","slug":"constructor-for-type-threw-an-exception","errorCode":null,"errorMessage":"Constructor for ${type} threw an exception","messagePattern":"Constructor for (.+?) threw an exception","errorType":"exception","errorClass":"ObjectAccessException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/config/util/ReflectionProvider.java","lineNumber":80,"sourceCode":"                }\n            }\n            if (Serializable.class.isAssignableFrom(type)) {\n                return instantiateUsingSerialization(type);\n            } else {\n                throw new ObjectAccessException(\"Cannot construct \" + type.getName()\n                        + \" as it does not have a no-args constructor\");\n            }\n        } catch (InstantiationException e) {\n            throw new ObjectAccessException(\"Cannot construct \" + type.getName(), e);\n        } catch (IllegalAccessException e) {\n            throw new ObjectAccessException(\"Cannot construct \" + type.getName(), e);\n        } catch (InvocationTargetException e) {\n            if (e.getTargetException() instanceof RuntimeException) {\n                throw (RuntimeException) e.getTargetException();\n            } else if (e.getTargetException() instanceof Error) {\n                throw (Error) e.getTargetException();\n            } else {\n                throw new ObjectAccessException(\"Constructor for \" + type.getName() + \" threw an exception\",\n                        e.getTargetException());\n            }\n        }\n    }\n\n    public void visitSerializableFields(Object object, Visitor visitor) {\n        for (Iterator<Field> iterator = fieldDictionary.serializableFieldsFor(object.getClass()); iterator.hasNext();) {\n            Field field = iterator.next();\n            if (!fieldModifiersSupported(field)) {\n                continue;\n            }\n            validateFieldAccess(field);\n            try {\n                Object value = field.get(object);\n                visitor.visit(field.getName(), field.getType(), field.getDeclaringClass(), value);\n            } catch (IllegalArgumentException e) {\n                throw new ObjectAccessException(\"Could not get field \" + field.getClass() + \".\" + field.getName(), e);\n            } catch (IllegalAccessException e) {","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/config/util/ReflectionProvider.java#L62-L98","documentation":"When Constructor.newInstance() throws InvocationTargetException, the target constructor itself threw. If the cause is a RuntimeException or Error it is rethrown as-is; otherwise it is wrapped in ObjectAccessException('Constructor for <type> threw an exception'). The real problem is inside the class's constructor body.","triggerScenarios":"Calling newInstance(type) where the type's no-arg constructor throws a checked exception (wrapped here) or, uncaught, a RuntimeException/Error passed straight through — e.g. init code reading files, connecting, or validating state in the constructor.","commonSituations":"Bean constructors that perform I/O or validation and fail due to bad environment (missing file, bad property); constructor throwing NPE/IllegalStateException on malformed inputs; config values the constructor rejects.","solutions":["Inspect the cause (getTargetException / printStackTrace) to find the exception thrown inside the constructor","Fix the class's constructor to not throw (move initialization out of the constructor or handle the failure)","Correct the environment/config value the constructor depends on (file path, property, external resource)"],"exampleFix":"// before\npublic Cfg() { load(new FileInputStream(path)); } // throws if file missing\n// after\npublic Cfg() { File f = new File(path); if (!f.exists()) { log.warn(\"no cfg file, defaults\"); return; } load(new FileInputStream(f)); }","handlingStrategy":"try-catch","validationCode":"try { type.getDeclaredConstructor().newInstance(); } catch (Throwable t) { throw new ConfigException(\"constructor of \" + type.getName() + \" fails in this environment: \" + t.getCause(), t); }","typeGuard":null,"tryCatchPattern":"try { return ReflectionProvider.newInstance(type); } catch (ObjectAccessException e) { log.error(\"ctor of {} threw: \", type.getName(), e.getCause()); throw e; }","preventionTips":["Keep constructors free of I/O, network, and validation logic","Always inspect getCause()/initCause chain — the wrapped exception is the real error","Test bean instantiation in CI with production-like environment"],"tags":["reflection","constructor","wrapped-exception"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}