{"record":{"id":"56ead695d7935046","repo":"JessYanCoding/AndroidAutoSize","slug":"you-should-init-first","errorCode":null,"errorMessage":"you should init first","messagePattern":"you should init first","errorType":"exception","errorClass":"java.lang.NullPointerException","httpStatus":null,"severity":"critical","filePath":"autosize/src/main/java/me/jessyan/autosize/utils/AutoSizeUtils.java","lineNumber":67,"sourceCode":"        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, value, context.getResources().getDisplayMetrics()) + 0.5f);\n    }\n\n    public static int in2px(Context context, float value) {\n        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, value, context.getResources().getDisplayMetrics()) + 0.5f);\n    }\n\n    public static int mm2px(Context context, float value) {\n        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, value, context.getResources().getDisplayMetrics()) + 0.5f);\n    }\n    \n    public static Application getApplicationByReflect() {\n        try {\n            @SuppressLint(\"PrivateApi\")\n            Class<?> activityThread = Class.forName(\"android.app.ActivityThread\");\n            Object thread = activityThread.getMethod(\"currentActivityThread\").invoke(null);\n            Object app = activityThread.getMethod(\"getApplication\").invoke(thread);\n            if (app == null) {\n                throw new NullPointerException(\"you should init first\");\n            }\n            return (Application) app;\n        } catch (NoSuchMethodException e) {\n            e.printStackTrace();\n        } catch (IllegalAccessException e) {\n            e.printStackTrace();\n        } catch (InvocationTargetException e) {\n            e.printStackTrace();\n        } catch (ClassNotFoundException e) {\n            e.printStackTrace();\n        }\n        throw new NullPointerException(\"you should init first\");\n    }\n}\n","sourceCodeStart":49,"sourceCodeEnd":82,"githubUrl":"https://github.com/JessYanCoding/AndroidAutoSize/blob/e402ecdd998ea07549513ac08c12e9f097f3b48c/autosize/src/main/java/me/jessyan/autosize/utils/AutoSizeUtils.java#L49-L82","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Defer the call until after Application.onCreate","Pass and cache a real Application reference at AutoSize.init time instead of relying on reflection","In tests, inject a mock Application rather than calling the reflective getter","Wrap the call and fall back to an Application provided by your own Application class"],"exampleFix":"// before\nApplication app = AutoSizeUtils.getApplicationByReflect(); // called in attachBaseContext/static init\n// after\npublic class MyApp extends Application {\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        AutoSize.initCompat(this); // app instance is available here\n    }\n}","handlingStrategy":"validation","validationCode":"Application app = (applicationContext instanceof Application) ? (Application) applicationContext : null;\nif (app == null) { /* defer until Application.onCreate */ }","typeGuard":"boolean isAppReady() { try { return AutoSizeUtils.getApplicationByReflect() != null; } catch (Exception e) { return false; } }","tryCatchPattern":"try { Application app = AutoSizeUtils.getApplicationByReflect(); } catch (NullPointerException e) { /* not yet initialized: defer to Application.onCreate or use cached app */ }","preventionTips":["Initialize AutoSize only in Application.onCreate","Avoid calling adaptation APIs from ContentProviders or static initializers","Cache the Application instance from your own Application class","In tests, inject a mock Application instead of using reflection"],"tags":["java","android","reflection","initialization-order"],"backgroundTag":"invalid-state-transition","analyzedSha":"e402ecdd998ea07549513ac08c12e9f097f3b48c","analyzedAt":"2026-09-07T15:05:42.094Z","contentChangedAt":"2026-09-07T15:05:42.094Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}