{"record":{"id":"55999d2e3672645c","repo":"stride3d/stride","slug":"value-cannot-be-null-parameter-canvas","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'canvas')","messagePattern":"Value cannot be null\\. \\(Parameter 'canvas'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Drawing/CanvasRenderer.cs","lineNumber":61,"sourceCode":"\nnamespace Stride.Core.Presentation.Drawing\n{\n    using Color = Stride.Core.Mathematics.Color;\n\n    public class CanvasRenderer : IDrawingContext\n    {\n        private readonly Dictionary<Color, Brush> cachedBrushes = new Dictionary<Color, Brush>();\n        private const int MaxPolylinesPerLine = 64;\n        private const int MinPointsPerPolyline = 16;\n\n        /// <summary>\n        /// The clip rectangle.\n        /// </summary>\n        private Rect? clip;\n\n        public CanvasRenderer([NotNull] Canvas canvas)\n        {\n            if (canvas == null) throw new ArgumentNullException(nameof(canvas));\n            Canvas = canvas;\n            UseStreamGeometry = true;\n        }\n\n        /// <summary>\n        /// Gets or sets the thickness limit for \"balanced\" line drawing.\n        /// </summary>\n        public double BalancedLineDrawingThicknessLimit { get; set; } = 3.5;\n\n        [NotNull]\n        public Canvas Canvas { get; }\n\n        /// <summary>\n        /// Gets or sets a value indicating whether to use stream geometry for lines and polygons rendering.\n        /// </summary>\n        /// <value><c>true</c> if stream geometry should be used; otherwise, <c>false</c> .</value>\n        /// <remarks>Using stream geometry seems to be slightly faster than using path geometry.</remarks>\n        public bool UseStreamGeometry { get; set; }","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Drawing/CanvasRenderer.cs#L43-L79","documentation":"The CanvasRenderer constructor requires a non-null Canvas and throws ArgumentNullException(nameof(canvas)) when null. CanvasRenderer wraps a WPF Canvas as the target of drawing commands (IDrawableRenderer); without a canvas nothing can be rendered, so the library fails fast at construction.","triggerScenarios":"Constructing CanvasRenderer with a null canvas — e.g. a Canvas field/property not yet assigned (XAML not yet loaded), a FindName/FindResource lookup returning null, or passing a null from a factory or data-binding that resolved before the visual tree was built.","commonSituations":"Instantiating the renderer in a control's constructor before InitializeComponent populated the canvas; code-behind using FindName with a mismatched x:Name; MVVM scenarios where the renderer is created before the view's visual tree exists.","solutions":["Ensure the Canvas is initialized (after InitializeComponent / Loaded) before constructing CanvasRenderer.","Null-check the canvas at the construction site and defer renderer creation until the canvas is available.","If the canvas is looked up by name, verify the x:Name matches and the element is in the visual/logical tree.","Use lazy creation: create the renderer on first draw call, when the canvas is guaranteed to exist."],"exampleFix":"// before\npublic MyControl()\n{\n    InitializeComponent();\n    renderer = new CanvasRenderer(canvas); // canvas may be null if lookup failed\n}\n// after\npublic MyControl()\n{\n    InitializeComponent();\n}\nprotected override void OnLoaded(EventArgs e)\n{\n    base.OnLoaded(e);\n    if (canvas == null) throw new InvalidOperationException(\"Canvas not found.\");\n    renderer = new CanvasRenderer(canvas);\n}","handlingStrategy":"validation","validationCode":"if (canvas == null) throw new InvalidOperationException(\"Canvas must be resolved before creating CanvasRenderer.\");","typeGuard":"static bool CanCreateRenderer(Canvas c) => c is not null;","tryCatchPattern":"try { renderer = new CanvasRenderer(canvas); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"canvas\") { log.Error(\"Canvas was null at renderer creation\"); }","preventionTips":["Create the renderer after InitializeComponent/Loaded, not in constructors.","Verify x:Name/FindName results before use.","Lazy-initialize the renderer on first draw.","In MVVM, wire renderer creation to the view's Loaded event."],"tags":["wpf","argument-null","constructor","canvas"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}