didi/DoKit · error · ReflectedException

name + " was null!"

Error message

name + " was null!"

What it means

Reflector.check(caller, member, name) throws '<name> was null!' (e.g. 'Field was null!' / 'Method was null!') when the member passed in is null. For field/method operations this means field(...) or method(...) was never called (or was reset by a subsequent constructor()/method()/field() call), so mField/mMethod is null at get/set/call time.

Source

Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/util/Reflector.java:101

        try {
            return (R) mConstructor.newInstance(initargs);
        } catch (InvocationTargetException e) {
            throw new ReflectedException("Oops!", e.getTargetException());
        } catch (Throwable e) {
            throw new ReflectedException("Oops!", e);
        }
    }
    
    protected Object checked(@Nullable Object caller) throws ReflectedException {
        if (caller == null || mType.isInstance(caller)) {
            return caller;
        }
        throw new ReflectedException("Caller [" + caller + "] is not a instance of type [" + mType + "]!");
    }
    
    protected void check(@Nullable Object caller, @Nullable Member member, @NonNull String name) throws ReflectedException {
        if (member == null) {
            throw new ReflectedException(name + " was null!");
        }
        if (caller == null && !Modifier.isStatic(member.getModifiers())) {
            throw new ReflectedException("Need a caller!");
        }
        checked(caller);
    }
    
    public Reflector bind(@Nullable Object caller) throws ReflectedException {
        mCaller = checked(caller);
        return this;
    }
    
    public Reflector unbind() {
        mCaller = null;
        return this;
    }
    
    public Reflector field(@NonNull String name) throws ReflectedException {

View on GitHub (pinned to 626827cddb)

Solutions

  1. Chain the lookup and the operation together: on(cls).field("x").get() — don't split them
  2. Never continue after a field()/method() call has thrown; the instance may hold null members
  3. Create a separate Reflector instance per member you need

Example fix

// before
Reflector r = Reflector.on(cls);
r.get(); // Field was null!

// after
Object v = Reflector.on(cls).field("mName").get();
Defensive patterns

Strategy: validation

Validate before calling

// one member per instance, chained immediately
Reflector r = Reflector.on(cls).field("name"); // then r.get() — never reuse r for other members

Prevention

When it happens

Trigger: Calling get()/set() without a prior successful field(name); calling call()/callByCaller() without method(name); calling .constructor(...) after .field(...) (resets mField to null) and then get(); a failed field()/method() lookup that threw and left the old member cleared on a partially mutated instance.

Common situations: Fluent chains reordered or split across helper methods; swallowing a ReflectedException from field() and continuing to get(); interleaving multiple member lookups on one Reflector instance.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/b330a2d4bb7dfe73. Report an issue: GitHub.