Tencent/matrix · error · IllegalArgumentException
unable to cast object
Error message
unable to cast object
What it means
The static-field variant of ReflectFiled.get() casts mField.get(null) to the generic Type and converts a ClassCastException into IllegalArgumentException("unable to cast object"). It fires when the actual field value's runtime type is not assignable to the declared generic Type.
Solutions
- Align the generic type parameter with the field's actual declared type
- Verify the field's type via mField.getType()/Field#getClass of the value before casting
- Use getWithoutThrow() to receive null instead of an exception on mismatch
- Catch IllegalArgumentException around the get() call and handle the fallback path
Example fix
// before ReflectFiled<String> f = new ReflectFiled<>(Foo.class, "count"); // count is int String v = f.get(); // after ReflectFiled<Integer> f = new ReflectFiled<>(Foo.class, "count"); Integer v = f.get();
Defensive patterns
Strategy: type-guard
Validate before calling
Field f = Foo.class.getDeclaredField("count");
if (!Number.class.isAssignableFrom(f.getType())) {
// adjust the generic type parameter of ReflectFiled
} Type guard
Object raw = Foo.class.getField(name).get(null);
if (raw instanceof String) { /* safe to use ReflectFiled<String> */ } Try / catch
try {
Type v = reflectFiled.get();
} catch (IllegalArgumentException e) {
if ("unable to cast object".equals(e.getMessage())) {
v = defaultValue;
} else { throw e; }
} Prevention
- Match ReflectFiled's generic parameter to the field's declared type
- Prefer getWithoutThrow() when a mismatch should not crash
- Re-check field types after upgrading the reflected library
When it happens
Trigger: Calling get() where the resolved static field's value cannot be cast to the generic type parameter used when constructing ReflectFiled (e.g. declared Type=String but field holds an Integer).
Common situations: Type parameters inferred incorrectly (raw types, unchecked casts); field type changed between library/app versions; using ReflectFiled against a field whose type was assumed rather than verified.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Both of invoker and fieldName can not be null or nil.
- Method is not exists.
- Both of invoker and fieldName can not be null or nil.
- Method is not exists.
- Can not get ClassLoader of
AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08).
Data as JSON: /api/errors/6000b4d514d14979.
Report an issue: GitHub.
Appendix: source
Thrown at matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/util/ReflectFiled.java:61
return get(false);
}
@SuppressWarnings("unchecked")
public synchronized Type get(boolean ignoreFieldNoExist)
throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
prepare();
if (mField == null) {
if (!ignoreFieldNoExist) {
throw new NoSuchFieldException();
}
MatrixLog.w(TAG, String.format("Field %s is no exists.", mFieldName));
return null;
}
Type fieldVal = null;
try {
fieldVal = (Type) mField.get(null);
} catch (ClassCastException e) {
throw new IllegalArgumentException("unable to cast object");
}
return fieldVal;
}
public synchronized Type get(boolean ignoreFieldNoExist, Object instance)
throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
prepare();
if (mField == null) {
if (!ignoreFieldNoExist) {
throw new NoSuchFieldException();
}
MatrixLog.w(TAG, String.format("Field %s is no exists.", mFieldName));
return null;
}
Type fieldVal = null;
try {
fieldVal = (Type) mField.get(instance);
} catch (ClassCastException e) {View on GitHub (pinned to 3b8293bd65)