{"record":{"id":"de5cc6ca20fb2863","repo":"SubtitleEdit/subtitleedit","slug":"vlcplayer-is-not-initialized","errorCode":null,"errorMessage":"VlcPlayer is not initialized","messagePattern":"VlcPlayer is not initialized","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/VideoPlayers/LibVlcDynamic/LibVlcDynamicSoftwareControl.cs","lineNumber":44,"sourceCode":"    private int _frameWidth;\n    private int _frameHeight;\n\n    public LibVlcDynamicPlayer? Player => _vlcPlayer;\n\n    public LibVlcDynamicSoftwareControl(LibVlcDynamicPlayer vlcPlayer)\n    {\n        _vlcPlayer = vlcPlayer;\n        ClipToBounds = true;\n        Cursor = new Cursor(StandardCursorType.Arrow);\n    }\n\n    protected override void OnInitialized()\n    {\n        base.OnInitialized();\n\n        if (_vlcPlayer == null)\n        {\n            throw new InvalidOperationException(\"VlcPlayer is not initialized\");\n        }\n\n        System.Diagnostics.Debug.WriteLine(\"Initializing VlcPlayer with software rendering\");\n\n        try\n        {\n            _vlcPlayer.LoadLib();\n            _vlcPlayer.PlayerSubName = \"sw\";\n            SetupVideoCallbacks();\n            _isInitialized = true;\n            System.Diagnostics.Debug.WriteLine(\"VlcPlayer initialized successfully with software rendering!\");\n        }\n        catch (Exception ex)\n        {\n            System.Diagnostics.Debug.WriteLine($\"Failed to initialize VlcPlayer: {ex.Message}\");\n        }\n    }\n","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/VideoPlayers/LibVlcDynamic/LibVlcDynamicSoftwareControl.cs#L26-L62","documentation":"Thrown by LibVlcDynamicSoftwareControl.OnInitialized() if the _vlcPlayer field is null when Avalonia raises the Initialized event. The field is assigned in the constructor from a non-nullable parameter, so under normal use it is never null here. This guard catches two abnormal cases: the control was constructed by passing null (a nullable-reference violation by the caller), or the control was detached (OnDetachedFromVisualTree sets _vlcPlayer = null) and then re-initialized in the same lifecycle.","triggerScenarios":"OnInitialized() fires after the Avalonia control is added to the logical tree. The throw occurs if _vlcPlayer is null at that point — either because 'new LibVlcDynamicSoftwareControl(null!)' was used, or because OnDetachedFromVisualTree already nulled the field (line 156) and the control is being re-hosted without a fresh instance.","commonSituations":"A caller bypasses the constructor contract and passes null (often from a DI/factory path that returned null); XAML/code re-parents the same control instance after detach, hitting Initialized again on a disposed player; a test instantiates the control without supplying a player. The downstream LoadLib/SetupVideoCallbacks would NRE without this explicit guard.","solutions":["Always construct LibVlcDynamicSoftwareControl with a non-null LibVlcDynamicPlayer obtained from the video-player factory.","Do not re-use a control instance after it has been detached — create a new LibVlcDynamicPlayer + control pair on each attach.","In DI/factory code, assert the resolved player is non-null before constructing the control.","If the player can legitimately be unavailable, branch on that before creating the control rather than passing null."],"exampleFix":"// before: factory may hand back null and the control is built anyway\nvar player = ResolvePlayer();\nvar control = new LibVlcDynamicSoftwareControl(player!);\n\n// after: fail fast at the factory boundary, never let null reach the control\nvar player = ResolvePlayer() ?? throw new InvalidOperationException(\"No LibVlcDynamicPlayer available.\");\nvar control = new LibVlcDynamicSoftwareControl(player);","handlingStrategy":"validation","validationCode":"// Guarantee a non-null player before the control is created.\nvar player = playerFactory.Create()\n    ?? throw new InvalidOperationException(\"No LibVlcDynamicPlayer available for software control.\");\nvar control = new LibVlcDynamicSoftwareControl(player);\n\n// Optional: assert at attach time if you re-host controls.\nif (control.Player is null)\n{\n    throw new InvalidOperationException(\"Cannot host a software control without a player.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass null into the LibVlcDynamicSoftwareControl constructor; resolve the player from a factory that itself throws on null.","Treat a control as single-use: once OnDetachedFromVisualTree nulled the player, create a fresh player+control pair on re-attach instead of reusing the instance.","Enable nullable reference types so a null player is a compile-time warning at the call site, not a runtime throw in OnInitialized."],"tags":["avalonia","libvlc","video-player","null-check","control-lifecycle"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}