{"record":{"id":"9ab8781a111db5a0","repo":"SubtitleEdit/subtitleedit","slug":"mpvplayer-is-not-initialized","errorCode":null,"errorMessage":"MpvPlayer is not initialized","messagePattern":"MpvPlayer is not initialized","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/VideoPlayers/LibMpvDynamic/LibMpvDynamicSoftwareControl.cs","lineNumber":35,"sourceCode":"    private WriteableBitmap? _renderTarget;\n    private bool _isInitialized;\n\n    public LibMpvDynamicPlayer? Player => _mpvPlayer;\n\n    public LibMpvDynamicSoftwareControl(LibMpvDynamicPlayer mpvPlayer)\n    {\n        _mpvPlayer = mpvPlayer;\n        ClipToBounds = true;\n        Cursor = new Cursor(StandardCursorType.Arrow);\n    }\n\n    protected override void OnInitialized()\n    {\n        base.OnInitialized();\n\n        if (_mpvPlayer == null)\n        {\n            throw new InvalidOperationException(\"MpvPlayer is not initialized\");\n        }\n\n        System.Diagnostics.Debug.WriteLine(\"Initializing MpvPlayer with software rendering\");\n\n        try\n        {\n            _mpvPlayer.InitializeWithSoftwareRendering();\n            _mpvPlayer.PlayerSubName = \"sw\";\n            _mpvPlayer.RequestRender += OnMpvRequestRender;\n            _isInitialized = true;\n            System.Diagnostics.Debug.WriteLine(\"MpvPlayer initialized successfully with software rendering!\");\n        }\n        catch (Exception ex)\n        {\n            System.Diagnostics.Debug.WriteLine($\"Failed to initialize MpvPlayer: {ex.Message}\");\n        }\n    }\n","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/VideoPlayers/LibMpvDynamic/LibMpvDynamicSoftwareControl.cs#L17-L53","documentation":"InvalidOperationException from OnInitialized: the control's _mpvPlayer dependency is null. The Avalonia control was instantiated (e.g. by the XAML designer/previewer, or without dependency injection) before a player instance was supplied.","triggerScenarios":"The XAML designer or previewer instantiates the control with no DI; the control is constructed before its mpvPlayer is injected; or the player service was never registered in the DI container.","commonSituations":"Design-time preview, a wiring bug where the player was not passed to the control, or a refactor that dropped the DI registration.","solutions":["Ensure the mpvPlayer is provided to the control before it initializes (resolve via the app's service provider / DI).","Guard OnInitialized with a design-mode check so the designer does not trigger initialization.","Register the player service in the application's DI container."],"exampleFix":"// before\nprotected override void OnInitialized()\n{\n    base.OnInitialized();\n    if (_mpvPlayer == null) throw new InvalidOperationException(\"MpvPlayer is not initialized\");\n    _mpvPlayer.InitializeWithSoftwareRendering();\n}\n\n// after\nprotected override void OnInitialized()\n{\n    base.OnInitialized();\n    if (Design.IsDesignMode || _mpvPlayer == null) return;\n    _mpvPlayer.InitializeWithSoftwareRendering();\n}","handlingStrategy":"validation","validationCode":"if (Design.IsDesignMode || _mpvPlayer == null) return;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Register the player service in the DI container.","Guard OnInitialized with Design.IsDesignMode to protect the designer.","Ensure the mpvPlayer is injected before the control initializes.","Fail fast with a clear message if the dependency is missing at runtime."],"tags":["mpv","avalonia","di","designer","null-check"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}