JessYanCoding/AndroidAutoSize · critical · java.lang.NullPointerException

you should init first

Error message

you should init first

What it means

getApplicationByReflect() obtains the Application via reflection on android.app.ActivityThread. If ActivityThread.currentActivityThread().getApplication() returns null, it throws NullPointerException("you should init first"), meaning the framework Application hasn't been created yet — the code ran before application initialization.

Solutions

  1. Defer the call until after Application.onCreate
  2. Pass and cache a real Application reference at AutoSize.init time instead of relying on reflection
  3. In tests, inject a mock Application rather than calling the reflective getter
  4. Wrap the call and fall back to an Application provided by your own Application class

Example fix

// before
Application app = AutoSizeUtils.getApplicationByReflect(); // called in attachBaseContext/static init
// after
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        AutoSize.initCompat(this); // app instance is available here
    }
}
Defensive patterns

Strategy: validation

Validate before calling

Application app = (applicationContext instanceof Application) ? (Application) applicationContext : null;
if (app == null) { /* defer until Application.onCreate */ }

Type guard

boolean isAppReady() { try { return AutoSizeUtils.getApplicationByReflect() != null; } catch (Exception e) { return false; } }

Try / catch

try { Application app = AutoSizeUtils.getApplicationByReflect(); } catch (NullPointerException e) { /* not yet initialized: defer to Application.onCreate or use cached app */ }

Prevention

When it happens

Trigger: Invoking getApplicationByReflect() (directly or via AutoSizeUtils helpers like dp2px on unusual paths) before Application.onCreate, e.g. in ContentProvider init, static initializers, or Unit tests where ActivityThread doesn't exist properly.

Common situations: Robolectric/unit tests, code running from a ContentProvider that starts before Application.onCreate, or multi-process setups where the process has no Application bound yet.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of JessYanCoding/AndroidAutoSize@e402ecdd99 (2026-09-07). Data as JSON: /api/errors/56ead695d7935046. Report an issue: GitHub.

Appendix: source

Thrown at autosize/src/main/java/me/jessyan/autosize/utils/AutoSizeUtils.java:67

        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, value, context.getResources().getDisplayMetrics()) + 0.5f);
    }

    public static int in2px(Context context, float value) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, value, context.getResources().getDisplayMetrics()) + 0.5f);
    }

    public static int mm2px(Context context, float value) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, value, context.getResources().getDisplayMetrics()) + 0.5f);
    }
    
    public static Application getApplicationByReflect() {
        try {
            @SuppressLint("PrivateApi")
            Class<?> activityThread = Class.forName("android.app.ActivityThread");
            Object thread = activityThread.getMethod("currentActivityThread").invoke(null);
            Object app = activityThread.getMethod("getApplication").invoke(thread);
            if (app == null) {
                throw new NullPointerException("you should init first");
            }
            return (Application) app;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        throw new NullPointerException("you should init first");
    }
}

View on GitHub (pinned to e402ecdd99)