{"record":{"id":"29db3318c2c4f9c5","repo":"Tencent/VasSonic","slug":"sonicdbhelper-createinstance-needs-to-be-called","errorCode":null,"errorMessage":"SonicDBHelper::createInstance() needs to be called before SonicDBHelper::getInstance()!","messagePattern":"SonicDBHelper::createInstance\\(\\) needs to be called before SonicDBHelper::getInstance\\(\\)!","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sonic-android/sdk/src/main/java/com/tencent/sonic/sdk/SonicDBHelper.java","lineNumber":66,"sourceCode":"\n    private static SonicDBHelper sInstance = null;\n\n    private static AtomicBoolean isDBUpgrading = new AtomicBoolean(false);\n\n    private SonicDBHelper(Context context) {\n        super(context, SONIC_DATABASE_NAME, null, SONIC_DATABASE_VERSION);\n    }\n\n    static synchronized SonicDBHelper createInstance(Context context) {\n        if (null == sInstance) {\n            sInstance = new SonicDBHelper(context);\n        }\n        return sInstance;\n    }\n\n    public static synchronized SonicDBHelper getInstance() {\n        if (null == sInstance) {\n            throw new IllegalStateException(\"SonicDBHelper::createInstance() needs to be called before SonicDBHelper::getInstance()!\");\n        }\n        return sInstance;\n    }\n\n    /**\n     * Called when the database is created for the first time. This is where the\n     * creation of tables and the initial population of the tables should happen.\n     *\n     * @param db The database.\n     */\n    @Override\n    public void onCreate(SQLiteDatabase db) {\n        // create sessionData table\n        db.execSQL(SonicDataHelper.CREATE_TABLE_SQL);\n\n        // upgrade SP if need(session data save in SP on sdk 1.0)\n        onUpgrade(db, -1, SONIC_DATABASE_VERSION);\n","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/Tencent/VasSonic/blob/59936beff656d4b5718ff6444d6c5e001a2c5231/sonic-android/sdk/src/main/java/com/tencent/sonic/sdk/SonicDBHelper.java#L48-L84","documentation":"SonicDBHelper is a singleton whose instance must be explicitly created via createInstance(Context) before use. getInstance() throws this IllegalStateException if the singleton was never initialized, so callers cannot silently operate on a null DB handle.","triggerScenarios":"Calling SonicDBHelper.getInstance() before any call to SonicDBHelper.createInstance(), e.g. accessing the DB helper from a code path (background thread, ContentProvider, secondary process) that runs before the SDK's init in Application.onCreate().","commonSituations":"Initializing Sonic in a lazy spot instead of Application.onCreate(); multiple processes where only one ran createInstance; refactoring that removed the init call; accessing the DB helper from a service started independently of the app UI.","solutions":["Call SonicDBHelper.createInstance(context) once during app startup (Application.onCreate) before any use of getInstance()","Guard call sites: use SonicDBHelper.isInitialized()/null-check pattern or defer DB access until after SDK init","Ensure initialization happens in every process that touches the DB","Move init earlier in the startup sequence if a component (service/provider) may run before Application.onCreate finishes"],"exampleFix":"// before\nSonicDBHelper.getInstance().insert(...); // IllegalStateException\n// after\npublic class App extends Application {\n  @Override public void onCreate() {\n    super.onCreate();\n    SonicDBHelper.createInstance(this);\n  }\n}\nSonicDBHelper.getInstance().insert(...);","handlingStrategy":"validation","validationCode":"if (SonicDBHelper.getInstanceOrNull() == null) {\n  SonicDBHelper.createInstance(context);\n}\nSonicDBHelper.getInstance()....","typeGuard":"// Java: wrap access in a lazy initializer\nsynchronized (App.class) {\n  if (!SonicDBHelper.isInitialized()) SonicDBHelper.createInstance(app);\n}","tryCatchPattern":"try {\n  SonicDBHelper.getInstance().query(...);\n} catch (IllegalStateException e) {\n  SonicDBHelper.createInstance(context);\n  SonicDBHelper.getInstance().query(...);\n}","preventionTips":["Always call createInstance(context) in Application.onCreate","Do not access the DB helper from processes that never initialize Sonic","Add the init call to your app-startup checklist/boilerplate"],"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"}