{"record":{"id":"79d85d9bbc584959","repo":"Tencent/VasSonic","slug":"sonicengine-createinstance-needs-to-be-called-b","errorCode":null,"errorMessage":"SonicEngine::createInstance() needs to be called before SonicEngine::getInstance()","messagePattern":"SonicEngine::createInstance\\(\\) needs to be called before SonicEngine::getInstance\\(\\)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sonic-android/sdk/src/main/java/com/tencent/sonic/sdk/SonicEngine.java","lineNumber":77,"sourceCode":"    private final ConcurrentHashMap<String, SonicSession> runningSessionHashMap = new ConcurrentHashMap<String, SonicSession>(5);\n\n\n    private SonicEngine(SonicRuntime runtime, SonicConfig config) {\n        this.runtime = runtime;\n        this.config = config;\n    }\n\n    /**\n     * Returns a SonicEngine instance\n     * <p>\n     * Make sure {@link #createInstance(SonicRuntime, SonicConfig)} has been called.\n     *\n     * @return SonicEngine instance\n     * @throws IllegalStateException if {@link #createInstance(SonicRuntime, SonicConfig)} hasn't been called\n     */\n    public static synchronized SonicEngine getInstance() {\n        if (null == sInstance) {\n            throw new IllegalStateException(\"SonicEngine::createInstance() needs to be called before SonicEngine::getInstance()\");\n        }\n        return sInstance;\n    }\n\n    /**\n     * Check if {@link #getInstance()} is ready or not.\n     * <p><b>Note: {@link #createInstance(SonicRuntime, SonicConfig)} must be called if {@code false} is returned.</b></p>\n     * @return\n     *      Return <code>true</code> if {@link #sInstance} is not null, <code>false</code> otherwise\n     */\n    public static synchronized boolean isGetInstanceAllowed() {\n        return null != sInstance;\n    }\n\n    /**\n     * Create SonicEngine instance. Meanwhile it will initialize engine and SonicRuntime.\n     * @param runtime SonicRuntime\n     * @param config SonicConfig","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/Tencent/VasSonic/blob/59936beff656d4b5718ff6444d6c5e001a2c5231/sonic-android/sdk/src/main/java/com/tencent/sonic/sdk/SonicEngine.java#L59-L95","documentation":"SonicEngine is the central singleton of the Sonic SDK and must be initialized with createInstance(new SonicRuntime..., SonicConfig) before use. getInstance() throws this IllegalStateException when the engine was never created, preventing use of an uninitialized engine.","triggerScenarios":"Calling SonicEngine.getInstance() (directly or indirectly, e.g. via makeSessionId) before SonicEngine.createInstance(runtime, config) has completed.","commonSituations":"Skipping SDK init in Application.onCreate; calling Sonic APIs from another thread/process before init; unit tests that exercise Sonic APIs without a runtime; initializing Sonic in an Activity that isn't the entry point.","solutions":["Call SonicEngine.createInstance(new SonicRuntimeImpl(application), sonicConfig) once in Application.onCreate before any Sonic API use","Use SonicEngine.isInstanceCreated() to gate call sites or lazily create the engine before use","Ensure all processes that use Sonic perform their own createInstance","In tests, add a setup step that calls createInstance with a mock SonicRuntime"],"exampleFix":"// before\nString sid = SonicEngine.getInstance().makeSessionId(url); // throws\n// after\n@Override public void onCreate() {\n  SonicEngine.createInstance(new SonicRuntimeImpl(this), new SonicConfig.Builder().build());\n}\nString sid = SonicEngine.getInstance().makeSessionId(url);","handlingStrategy":"validation","validationCode":"if (!SonicEngine.isInstanceCreated()) {\n  SonicEngine.createInstance(new SonicRuntimeImpl(app), new SonicConfig.Builder().build());\n}","typeGuard":"// Java: central accessor\nstatic synchronized SonicEngine engine(Application app) {\n  if (!SonicEngine.isInstanceCreated())\n    SonicEngine.createInstance(new SonicRuntimeImpl(app), new SonicConfig.Builder().build());\n  return SonicEngine.getInstance();\n}","tryCatchPattern":"try {\n  SonicEngine.getInstance().makeSessionId(url);\n} catch (IllegalStateException e) {\n  SonicEngine.createInstance(runtime, config); // recover then retry once\n}","preventionTips":["Initialize the engine in Application.onCreate before any feature uses Sonic","Gate test code with a @Before that calls createInstance with a mock runtime","Remember per-process init for multi-process apps"],"tags":["android","singleton","initialization"],"backgroundTag":"module-init-failed","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"}