{"record":{"id":"b15394e54c6bbf90","repo":"Blankj/AndroidUtilCode","slug":"u-should-init-first","errorCode":null,"errorMessage":"u should init first","messagePattern":"u should init first","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"critical","filePath":"lib/subutil/src/main/java/com/blankj/subutil/util/Utils.java","lineNumber":90,"sourceCode":"\n    /**\n     * Return the context of Application object.\n     *\n     * @return the context of Application object\n     */\n    public static Application getApp() {\n        if (sApplication != null) return sApplication;\n        return getApplicationByReflect();\n    }\n\n    private static Application getApplicationByReflect() {\n        try {\n            @SuppressLint(\"PrivateApi\")\n            Class<?> activityThread = Class.forName(\"android.app.ActivityThread\");\n            Object at = activityThread.getMethod(\"currentActivityThread\").invoke(null);\n            Object app = activityThread.getMethod(\"getApplication\").invoke(at);\n            if (app == null) {\n                throw new NullPointerException(\"u should init first\");\n            }\n            init((Application) app);\n            return sApplication;\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(\"u should init first\");\n    }\n\n\n    public static final class ContentProvider4SubUtil extends ContentProvider {\n","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/subutil/src/main/java/com/blankj/subutil/util/Utils.java#L72-L108","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Call Utils.init(application) explicitly from your Application.onCreate() so getApp() never falls back to reflection.","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.","Deerate any Utils.getApp() call that runs before Application.onCreate (move it into lazy/late-init paths)."],"exampleFix":"// before: getApp() called in a ContentProvider.onCreate that beats Application.onCreate\nApplication app = Utils.getApp(); // reflect path -> app is null -> throws\n\n// after\npublic class App extends Application {\n    @Override public void onCreate() {\n        super.onCreate();\n        com.blankj.subutil.util.Utils.init(this);\n    }\n}","handlingStrategy":"validation","validationCode":"// Ensure early, explicit init so getApp() never reaches reflection.\npublic class App extends Application {\n    @Override public void onCreate() {\n        super.onCreate();\n        com.blankj.subutil.util.Utils.init(this);\n    }\n}\n// Then getApp() returns sApplication directly without reflection.","typeGuard":null,"tryCatchPattern":"try {\n    Application app = Utils.getApp();\n} catch (NullPointerException e) {\n    // Application not yet created in this process; defer or seed init\n}","preventionTips":["Call Utils.init(application) in Application.onCreate() so getApp() never reflects.","Avoid Utils.getApp() in code that runs before Application.onCreate (ContentProviders, early providers).","For non-main processes, ensure ContentProvider4SubUtil is registered or init explicitly."],"tags":["android","init","reflection","lifecycle","multiprocess"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}