{"record":{"id":"a1eacd7d1268641b","repo":"Tencent/matrix","slug":"matrix-init-matrix-should-not-be-null","errorCode":null,"errorMessage":"Matrix init, Matrix should not be null.","messagePattern":"Matrix init, Matrix should not be null\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/Matrix.java","lineNumber":63,"sourceCode":"        this.plugins = plugins;\n        MatrixLifecycleOwnerInitializer.init(app, config);\n        ProcessSupervisor.INSTANCE.init(app, config.getSupervisorConfig());\n        for (Plugin plugin : plugins) {\n            plugin.init(application, listener);\n        }\n    }\n\n    public static void setLogIml(MatrixLog.MatrixLogImp imp) {\n        MatrixLog.setMatrixLogImp(imp);\n    }\n\n    public static boolean isInstalled() {\n        return sInstance != null;\n    }\n\n    public static Matrix init(Matrix matrix) {\n        if (matrix == null) {\n            throw new RuntimeException(\"Matrix init, Matrix should not be null.\");\n        }\n        synchronized (Matrix.class) {\n            if (sInstance == null) {\n                sInstance = matrix;\n            } else {\n                MatrixLog.e(TAG, \"Matrix instance is already set. this invoking will be ignored\");\n            }\n        }\n        return sInstance;\n    }\n\n    public static Matrix with() {\n        if (sInstance == null) {\n            throw new RuntimeException(\"you must init Matrix sdk first\");\n        }\n        return sInstance;\n    }\n","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/Matrix.java#L45-L81","documentation":"Matrix.init(Matrix) requires a non-null Matrix instance to install as the SDK singleton. If you pass null, the library throws immediately because there is no instance to register. This is a fail-fast guard at SDK entry.","triggerScenarios":"Calling Matrix.init(null), e.g. when the Matrix instance is built conditionally or a factory returns null.","commonSituations":"Wiring Matrix in Application.onCreate with a nullable builder result; refactoring where the Matrix construction was removed but init call left; conditional feature flags that skip building Matrix.","solutions":["Pass a fully constructed Matrix (new Matrix.Builder(application)...build()) to Matrix.init.","Ensure any conditional construction path always returns a Matrix instance before calling init.","Only call Matrix.init after verifying the built object is non-null."],"exampleFix":"// before\nMatrix.init(buildMatrixOrNull(application));\n// after\nMatrix matrix = buildMatrixOrNull(application);\nif (matrix != null) {\n    Matrix.init(matrix);\n}","handlingStrategy":"validation","validationCode":"Matrix matrix = buildMatrix(application);\nif (matrix == null) throw new IllegalStateException(\"Matrix instance must not be null\");\nMatrix.init(matrix);","typeGuard":"boolean canInitMatrix(Matrix m) { return m != null; }","tryCatchPattern":"try {\n    Matrix.init(matrix);\n} catch (RuntimeException e) {\n    Log.e(TAG, \"Matrix init failed\", e);\n}","preventionTips":["Always build the Matrix via Builder before init","Never pass a possibly-null variable directly to Matrix.init","Initialize Matrix once in Application.onCreate"],"tags":["android","initialization","null-check"],"backgroundTag":"null-argument","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}