MyCATApache/Mycat-Server · error · ObjectAccessException

Could not get field .

Error message

Could not get field ${fieldClass}.${fieldName}

What it means

ReflectionProvider.visitSerializableFields reads each field's value reflectively and wraps IllegalArgumentException/IllegalAccessException from Field.get() in ObjectAccessException('Could not get field <class>.<name>'). Note it uses field.getClass() (the java.lang.reflect.Field class) rather than the declaring class, so the message shows java.lang.reflect.Field — a cosmetic bug — but the cause is an inaccessible or type-mismatched field read.

Solutions

  1. Ensure the object passed to visitSerializableFields is a proper instance whose class hierarchy contains the visited fields (pass the correct object, not null or a proxy)
  2. Make the fields accessible (public) or grant setAccessible permissions / correct JPMS module-opens
  3. Inspect the wrapped cause to determine whether it is IllegalArgumentException (wrong object) vs IllegalAccessException (access denied)

Example fix

// before
provider.visitSerializableFields(null, visitor); // IllegalArgumentException
// after
if (obj == null) { throw new IllegalArgumentException("object required"); }
provider.visitSerializableFields(obj, visitor);
Defensive patterns

Strategy: validation

Validate before calling

if (object == null) throw new IllegalArgumentException("visitSerializableFields: object is null");
if (!field.getDeclaringClass().isInstance(object)) throw new IllegalArgumentException(object.getClass() + " is not a " + field.getDeclaringClass());

Try / catch

try { provider.visitSerializableFields(obj, visitor); } catch (ObjectAccessException e) { log.error("field visit failed: {} (check cause)", e.getMessage(), e.getCause()); throw e; }

Prevention

When it happens

Trigger: Calling visitSerializableFields(object, visitor) where a field is final/static-incompatible for get(), the object is not an instance of the field's declaring class (IllegalArgumentException), or field access is blocked (IllegalAccessException).

Common situations: Serializing an object whose runtime class does not match the field's declaring class hierarchy; visiting fields on a proxy or mismatched instance; reflective access denied by module system/SecurityManager; passing null object.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


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

Appendix: source

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

            } 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) {
                throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName(), e);
            }
        }
    }

    public void writeField(Object object, String fieldName, Object value, Class<?> definedIn) {
        Field field = fieldDictionary.field(object.getClass(), fieldName, definedIn);
        validateFieldAccess(field);
        try {
            field.set(object, value);
        } catch (IllegalArgumentException e) {
            throw new ObjectAccessException("Could not set field " + field.getName() + "@" + object.getClass(), e);
        } catch (IllegalAccessException e) {
            throw new ObjectAccessException("Could not set field " + field.getName() + "@" + object.getClass(), e);
        }
    }

View on GitHub (pinned to 65f8d8beb7)