{"record":{"id":"cd3aa4f848ff5e34","repo":"Tencent/tinker","slug":"context-must-not-be-null","errorCode":null,"errorMessage":"Context must not be null.","messagePattern":"Context must not be null\\.","errorType":"exception","errorClass":"TinkerRuntimeException","httpStatus":null,"severity":"error","filePath":"tinker-android/tinker-android-lib-no-op/src/main/java/com/tencent/tinker/lib/tinker/Tinker.java","lineNumber":299,"sourceCode":"        private final Context context;\n        private final boolean mainProcess;\n        private final boolean patchProcess;\n\n        private int status = -1;\n        private LoadReporter  loadReporter;\n        private PatchReporter patchReporter;\n        private PatchListener listener;\n        private File          patchDirectory;\n        private File          patchInfoFile;\n        private File          patchInfoLockFile;\n        private Boolean       tinkerLoadVerifyFlag;\n\n        /**\n         * Start building a new {@link Tinker} instance.\n         */\n        public Builder(Context context) {\n            if (context == null) {\n                throw new TinkerRuntimeException(\"Context must not be null.\");\n            }\n            this.context = context;\n            this.mainProcess = TinkerServiceInternals.isInMainProcess(context);\n            this.patchProcess = TinkerServiceInternals.isInTinkerPatchServiceProcess(context);\n            this.patchDirectory = SharePatchFileUtil.getPatchDirectory(context);\n            if (this.patchDirectory == null) {\n                ShareTinkerLog.e(TAG, \"patchDirectory is null!\");\n                return;\n            }\n            this.patchInfoFile = SharePatchFileUtil.getPatchInfoFile(patchDirectory.getAbsolutePath());\n            this.patchInfoLockFile = SharePatchFileUtil.getPatchInfoLockFile(patchDirectory.getAbsolutePath());\n            ShareTinkerLog.w(TAG, \"tinker patch directory: %s\", patchDirectory);\n        }\n\n        public Builder tinkerFlags(int tinkerFlags) {\n            if (this.status != -1) {\n                throw new TinkerRuntimeException(\"tinkerFlag is already set.\");\n            }","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/Tencent/tinker/blob/1b7ea02c239840f563ea64fb5bd286eb98d4011e/tinker-android/tinker-android-lib-no-op/src/main/java/com/tencent/tinker/lib/tinker/Tinker.java#L281-L317","documentation":"Tinker.Builder's constructor throws TinkerRuntimeException(\"Context must not be null.\") when passed a null Context. The builder immediately uses the context to detect the current process and compute the patch directory, so a null context cannot be deferred; the guard fails fast at construction rather than later during build().","triggerScenarios":"new Tinker.Builder(null) — typically because the context field was not yet assigned (call from a constructor or static init before onCreate), or a helper received null and forwarded it.","commonSituations":"Custom Application subclasses calling the builder before super.onCreate()/attachBaseContext finished setting up fields; test harnesses passing a null or mocked-null context; refactor moving Tinker init into a class whose context argument became optional.","solutions":["Pass the application context obtained in attachBaseContext/onCreate (or ApplicationLike.getApplication()) — never an uninitialized field.","Null-check the context at the call site with a clear error naming the caller when migrating old code.","In unit tests, use RuntimeEnvironment.application (Robolectric) rather than null."],"exampleFix":"// before\npublic class MyApp extends Application {\n    private final Tinker.Builder b = new Tinker.Builder(context); // field init: context null\n\n// after\npublic class MyApp extends Application {\n    private Tinker.Builder b;\n    @Override public void onCreate() {\n        super.onCreate();\n        b = new Tinker.Builder(getApplicationContext());\n    }\n}","handlingStrategy":"validation","validationCode":"Context ctx = (application != null) ? application.getApplicationContext() : null;\nif (ctx == null) throw new IllegalStateException(\"no context for Tinker init\");\nTinker.Builder builder = new Tinker.Builder(ctx);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never build Tinker config from field initializers; build it in onCreate/attachBaseContext","Pass getApplicationContext(), not captured nullable fields"],"tags":["android","tinker","builder","null-check"],"backgroundTag":null,"analyzedSha":"1b7ea02c239840f563ea64fb5bd286eb98d4011e","analyzedAt":"2026-08-14T15:16:52.110Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}