{"record":{"id":"3f4ff2dc547357fa","repo":"Tencent/VasSonic","slug":"sonicruntime-context-con-not-be-null","errorCode":null,"errorMessage":"SonicRuntime context con not be null!","messagePattern":"SonicRuntime context con not be null!","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"sonic-android/sdk/src/main/java/com/tencent/sonic/sdk/SonicRuntime.java","lineNumber":59,"sourceCode":"\n    /**\n     * Log filter\n     */\n    private final static String TAG = SonicConstants.SONIC_SDK_LOG_PREFIX + \"SonicRuntime\";\n\n    /**\n     * A context for this runtime, it's expected to be ApplicationContext\n     */\n    protected final Context context;\n\n    /**\n     * This handle thread use to save sonic cache.\n     */\n    protected volatile static HandlerThread fileHandlerThread;\n\n    public SonicRuntime(Context context) {\n        if (null == context) {\n            throw new NullPointerException(\"SonicRuntime context con not be null!\");\n        }\n        this.context = context;\n    }\n\n    public Context getContext() {\n        return context;\n    }\n\n\n    /**\n     * Make a unique session id for the url, it can be account related.\n     * @param url Url which need to make session id\n     * @param isAccountRelated Is account related or not\n     * @return A unique session id\n     */\n    public String makeSessionId(String url, boolean isAccountRelated) {\n        if (isSonicUrl(url)) {\n            StringBuilder sessionIdBuilder = new StringBuilder();","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/Tencent/VasSonic/blob/59936beff656d4b5718ff6444d6c5e001a2c5231/sonic-android/sdk/src/main/java/com/tencent/sonic/sdk/SonicRuntime.java#L41-L77","documentation":"SonicRuntime is the abstract base providing app context and platform services to Sonic. Its constructor throws NullPointerException when passed a null Context, since nearly every runtime operation (file cache, network, main-thread handlers) requires a valid application context.","triggerScenarios":"Invoking a SonicRuntime subclass constructor (directly or via SonicEngine.createInstance) with a null Context argument, e.g. getActivity()/getApplication() returning null in a detached context or during early startup.","commonSituations":"Creating the runtime in a Fragment/View that is detached; calling createInstance before Application is attached; passing an uninitialized field; refactors that swap a lazy Context getter returning null.","solutions":["Pass the Application context (getApplicationContext()) to the runtime constructor instead of an Activity context","Ensure the runtime is constructed after the Application/Activity is fully attached (e.g. in Application.onCreate)","Null-check the context provider at the call site before constructing the runtime","Avoid constructing SonicRuntime in lifecycle callbacks where context may not exist (onAttach before, onDestroy after)"],"exampleFix":"// before\nSonicEngine.createInstance(new SonicRuntimeImpl(getActivity()), config); // NPE when detached\n// after\nContext appCtx = MyApplication.getInstance().getApplicationContext();\nSonicEngine.createInstance(new SonicRuntimeImpl(appCtx), config);","handlingStrategy":"validation","validationCode":"Context ctx = getActivity() != null ? getActivity().getApplicationContext() : MyApplication.getInstance();\nif (ctx == null) throw new IllegalStateException(\"No context available yet\");\nnew SonicRuntimeImpl(ctx);","typeGuard":"// Java\nstatic boolean hasContext(Fragment f) {\n  return f.isAdded() && f.getContext() != null;\n}","tryCatchPattern":"try {\n  SonicEngine.createInstance(new SonicRuntimeImpl(ctx), config);\n} catch (NullPointerException e) {\n  Log.e(TAG, \"Context was null; defer Sonic init to Application.onCreate\", e);\n}","preventionTips":["Always use getApplicationContext() for SDK singletons","Construct the runtime only in Application.onCreate","Never build the runtime from detached fragments or destroyed activities"],"tags":["android","null-pointer","context"],"backgroundTag":"null-argument","analyzedSha":"59936beff656d4b5718ff6444d6c5e001a2c5231","analyzedAt":"2026-09-08T10:27:05.448Z","contentChangedAt":"2026-09-08T10:27:05.448Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}