didi/DoKit · error · NullPointerException

mDb == null || mDb.mockApiDao()

Error message

mDb == null || mDb.mockApiDao()

What it means

Thrown inside DokitDbManager.getAllInterceptApis()'s background task when DoKitViewManager.getINSTANCE().getDb() returns null or the database's mockApiDao() is null. The message 'mDb == null || mDb.mockApiDao()' reflects both failure modes, but note the code itself dereferences db without a null check, so a null db actually NPEs at db.mockApiDao() first. The task runs on an IO thread via ThreadUtils; the throwable is delivered to onFail and logged, not rethrown to the caller.

Source

Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/kit/network/room_db/DokitDbManager.java:90

        private static DokitDbManager INSTANCE = new DokitDbManager();
    }

    public static DokitDbManager getInstance() {
        return DokitDbManager.Holder.INSTANCE;
    }

    /**
     * 获取所有的mock intercept apis
     */
    public void getAllInterceptApis() {
        ThreadUtils.executeByIo(new ThreadUtils.SimpleTask<List<T>>() {
            @Override
            public List<T> doInBackground() throws Throwable {
                DokitDatabase db = DoKitViewManager.getINSTANCE().getDb();
                if (db.mockApiDao() != null) {
                    return (List<T>) db.mockApiDao().getAllInterceptApi();
                } else {
                    throw new NullPointerException("mDb == null || mDb.mockApiDao()");
                }
            }

            @Override
            public void onSuccess(List<T> result) {
                list2mapByIntercept(result);
            }

            @Override
            public void onFail(Throwable t) {
                super.onFail(t);
                LogHelper.e(TAG, "error====>" + t.getMessage());
            }
        });
    }


    /**

View on GitHub (pinned to 626827cddb)

Solutions

  1. Ensure DoraemonKit.install(...) has completed and the DB is set before invoking mock data queries — check DoKitViewManager.getINSTANCE().getDb() != null first
  2. Retry the call after DB initialization completes, or defer it with a listener/post-init callback
  3. If mockApiDao() itself is null, inspect logcat for the earlier Room initialization/migration failure and fix that root cause

Example fix

// before
dokitDbManager.getAllInterceptApis(); // may run before db init

// after
if (DoKitViewManager.getINSTANCE().getDb() != null) {
    dokitDbManager.getAllInterceptApis();
} else {
    LogHelper.e(TAG, "DoKit db not ready; skip mock intercept query");
}
Defensive patterns

Strategy: validation

Validate before calling

DokitDatabase db = DoKitViewManager.getINSTANCE().getDb();
if (db != null && db.mockApiDao() != null) {
    dokitDbManager.getAllInterceptApis();
}

Type guard

boolean dbReady() { DokitDatabase db = DoKitViewManager.getINSTANCE().getDb(); return db != null && db.mockApiDao() != null; }

Prevention

When it happens

Trigger: Calling getAllInterceptApis() before the DoKit database has been initialized (DoKitViewManager.getINSTANCE().getDb() still null), typically at app startup or when DoKit was not fully installed; Room DAO creation failed or the DB schema/version migration failed, leaving mockApiDao() null

Common situations: Code or another kit querying mock-intercept data during Application.onCreate before DoKit init completes; DoKit installed but the DokitDatabase Room build threw earlier (schema/migration issue), leaving a half-initialized state; Race between DB init thread and the first query

Related errors


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