MyCATApache/Mycat-Server · error · ObjectAccessException

Constructor for threw an exception

Error message

Constructor for ${type} threw an exception

What it means

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.

Solutions

  1. Inspect the cause (getTargetException / printStackTrace) to find the exception thrown inside the constructor
  2. Fix the class's constructor to not throw (move initialization out of the constructor or handle the failure)
  3. Correct the environment/config value the constructor depends on (file path, property, external resource)

Example fix

// before
public Cfg() { load(new FileInputStream(path)); } // throws if file missing
// after
public Cfg() { File f = new File(path); if (!f.exists()) { log.warn("no cfg file, defaults"); return; } load(new FileInputStream(f)); }
Defensive patterns

Strategy: try-catch

Validate before calling

try { type.getDeclaredConstructor().newInstance(); } catch (Throwable t) { throw new ConfigException("constructor of " + type.getName() + " fails in this environment: " + t.getCause(), t); }

Try / catch

try { return ReflectionProvider.newInstance(type); } catch (ObjectAccessException e) { log.error("ctor of {} threw: ", type.getName(), e.getCause()); throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/8c8782b3e34912eb. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/config/util/ReflectionProvider.java:80

                }
            }
            if (Serializable.class.isAssignableFrom(type)) {
                return instantiateUsingSerialization(type);
            } else {
                throw new ObjectAccessException("Cannot construct " + type.getName()
                        + " as it does not have a no-args constructor");
            }
        } catch (InstantiationException e) {
            throw new ObjectAccessException("Cannot construct " + type.getName(), e);
        } catch (IllegalAccessException e) {
            throw new ObjectAccessException("Cannot construct " + type.getName(), e);
        } catch (InvocationTargetException e) {
            if (e.getTargetException() instanceof RuntimeException) {
                throw (RuntimeException) e.getTargetException();
            } else if (e.getTargetException() instanceof Error) {
                throw (Error) e.getTargetException();
            } else {
                throw new ObjectAccessException("Constructor for " + type.getName() + " threw an exception",
                        e.getTargetException());
            }
        }
    }

    public void visitSerializableFields(Object object, Visitor visitor) {
        for (Iterator<Field> iterator = fieldDictionary.serializableFieldsFor(object.getClass()); iterator.hasNext();) {
            Field field = iterator.next();
            if (!fieldModifiersSupported(field)) {
                continue;
            }
            validateFieldAccess(field);
            try {
                Object value = field.get(object);
                visitor.visit(field.getName(), field.getType(), field.getDeclaringClass(), value);
            } catch (IllegalArgumentException e) {
                throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName(), e);
            } catch (IllegalAccessException e) {

View on GitHub (pinned to 65f8d8beb7)