{"record":{"id":"f02a88585d74d79e","repo":"microg/GmsCore","slug":"null-application-context","errorCode":null,"errorMessage":"null application Context","messagePattern":"null application Context","errorType":"exception","errorClass":"LoadingException","httpStatus":null,"severity":"error","filePath":"play-services-basement/src/main/java/com/google/android/gms/dynamite/DynamiteModule.java","lineNumber":133,"sourceCode":"        } catch (Exception e) {\n            Log.e(TAG, \"Failed to load module descriptor class.\", e);\n            return 0;\n        }\n    }\n\n    public static int getRemoteVersion(@NonNull Context context, @NonNull String moduleId) {\n        return getRemoteVersion(context, moduleId, false);\n    }\n\n    public static int getRemoteVersion(@NonNull Context context, @NonNull String moduleId, boolean forceStaging) {\n        Log.e(TAG, \"Remote modules not yet supported\");\n        return 0;\n    }\n\n    @NonNull\n    public static DynamiteModule load(@NonNull Context context, @NonNull VersionPolicy policy, @NonNull String moduleId) throws LoadingException {\n        Context applicationContext = context.getApplicationContext();\n        if (applicationContext == null) throw new LoadingException(\"null application Context\", null);\n        try {\n            VersionPolicy.SelectionResult result = policy.selectModule(context, moduleId, VersionPolicy.IVersions.Default);\n            Log.i(TAG, \"Considering local module \" + moduleId + \":\" + result.localVersion + \" and remote module \" + moduleId + \":\" + result.remoteVersion);\n            switch (result.selection) {\n                case NONE:\n                    throw new LoadingException(\"No acceptable module \" + moduleId + \" found. Local version is \" + result.localVersion + \" and remote version is \" + result.remoteVersion + \".\");\n                case LOCAL:\n                    Log.i(TAG, \"Selected local version of \" + moduleId);\n                    return new DynamiteModule(context);\n                case REMOTE:\n                    throw new UnsupportedOperationException();\n                default:\n                    throw new LoadingException(\"VersionPolicy returned invalid code:\" + result.selection);\n            }\n        } catch (LoadingException loadingException) {\n            throw loadingException;\n        } catch (Throwable e) {\n            throw new LoadingException(\"Failed to load remote module.\", e);","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-basement/src/main/java/com/google/android/gms/dynamite/DynamiteModule.java#L115-L151","documentation":"DynamiteModule.load(context, policy, moduleId) first resolves context.getApplicationContext(); if that returns null it throws a LoadingException('null application Context'). Dynamite needs a real application context to query and load the dynamic module.","triggerScenarios":"Passing a bare/mock Context (e.g., Mockito-mocked Context, Robolectric context without application, or a ContextWrapper wrapping null) whose getApplicationContext() returns null.","commonSituations":"Unit or instrumentation tests injecting fake Contexts into gms APIs; early-lifecycle code calling load before the Application is attached; custom Context subclasses that override getApplicationContext() to return null.","solutions":["Pass a real, fully-attached Context: an Activity, a Service, or context.getApplicationContext() from a live component","In tests, use ApplicationProvider.getApplicationContext() (Robolectric) or a real instrumentation target context instead of a mocked Context","Never call DynamiteModule.load before the Application is created (e.g., not in a static initializer of a non-ContentProvider class)","Wrap load in try-catch LoadingException and degrade gracefully"],"exampleFix":"// before\nDynamiteModule.load(mockContext, policy, \"module\"); // throws\n// after\nContext appCtx = ApplicationProvider.getApplicationContext();\nDynamiteModule.load(appCtx, policy, \"module\");","handlingStrategy":"type-guard","validationCode":"if (context == null || context.getApplicationContext() == null) {\n    // do not call DynamiteModule.load with this context\n}","typeGuard":"boolean canLoadDynamite(Context ctx) {\n    return ctx != null && ctx.getApplicationContext() != null;\n}","tryCatchPattern":"try {\n    DynamiteModule m = DynamiteModule.load(ctx, policy, moduleId);\n} catch (DynamiteModule.LoadingException e) {\n    Log.w(TAG, \"Dynamite load failed\", e);\n}","preventionTips":["Always pass an Activity, Service, or a real application context","In tests use ApplicationProvider.getApplicationContext(), not mocked Contexts","Don't call DynamiteModule.load from static initializers before the Application exists"],"tags":["android","context","dynamite","lifecycle"],"backgroundTag":"null-argument","analyzedSha":"157c9d86ac46c195a86c2f15ab55c84036223f95","analyzedAt":"2026-09-06T17:27:33.892Z","contentChangedAt":"2026-09-06T17:27:33.892Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}