Blankj/AndroidUtilCode · critical · NullPointerException

u should init first

Error message

u should init first

What it means

When Utils.getApplicationByReflect cannot find a running Application via ActivityThread.currentActivityThread().getApplication(), it throws NullPointerException("u should init first"). This path is taken when the Application has not yet been created in the current process, so reflection returns a non-null ActivityThread but a null Application.

Source

Thrown at lib/subutil/src/main/java/com/blankj/subutil/util/Utils.java:90

    /**
     * Return the context of Application object.
     *
     * @return the context of Application object
     */
    public static Application getApp() {
        if (sApplication != null) return sApplication;
        return getApplicationByReflect();
    }

    private static Application getApplicationByReflect() {
        try {
            @SuppressLint("PrivateApi")
            Class<?> activityThread = Class.forName("android.app.ActivityThread");
            Object at = activityThread.getMethod("currentActivityThread").invoke(null);
            Object app = activityThread.getMethod("getApplication").invoke(at);
            if (app == null) {
                throw new NullPointerException("u should init first");
            }
            init((Application) app);
            return sApplication;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        throw new NullPointerException("u should init first");
    }


    public static final class ContentProvider4SubUtil extends ContentProvider {

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Call Utils.init(application) explicitly from your Application.onCreate() so getApp() never falls back to reflection.
  2. Ensure ContentProvider4SubUtil is registered in the manifest for the process (its onCreate calls Utils.init(getContext())), or avoid calling Utils.getApp() in processes that lack it.
  3. Deerate any Utils.getApp() call that runs before Application.onCreate (move it into lazy/late-init paths).

Example fix

// before: getApp() called in a ContentProvider.onCreate that beats Application.onCreate
Application app = Utils.getApp(); // reflect path -> app is null -> throws

// after
public class App extends Application {
    @Override public void onCreate() {
        super.onCreate();
        com.blankj.subutil.util.Utils.init(this);
    }
}
Defensive patterns

Strategy: validation

Validate before calling

// Ensure early, explicit init so getApp() never reaches reflection.
public class App extends Application {
    @Override public void onCreate() {
        super.onCreate();
        com.blankj.subutil.util.Utils.init(this);
    }
}
// Then getApp() returns sApplication directly without reflection.

Try / catch

try {
    Application app = Utils.getApp();
} catch (NullPointerException e) {
    // Application not yet created in this process; defer or seed init
}

Prevention

When it happens

Trigger: Calling Utils.getApp() (or Utils.init(null), which delegates here) from code that runs before the process's Application is constructed — e.g., another ContentProvider's onCreate that runs before the Application's onCreate, or from a non-main process where ActivityThread exists but getApplication() is still null.

Common situations: Multi-process apps (push, remote views) where ContentProvider4SubUtil or a third-party provider initialises utilcode before the process Application exists; early access during app startup ordering races; calling Utils.getApp() from a background process that was started without a custom Application.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/b15394e54c6bbf90. Report an issue: GitHub.