{"record":{"id":"702a263e2234becb","repo":"stride3d/stride","slug":"argumentnullexception-targetprofiles","errorCode":null,"errorMessage":"ArgumentNullException (targetProfiles)","messagePattern":"ArgumentNullException \\(targetProfiles\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/Direct3D12/GraphicsOutput.Direct3D12.cs","lineNumber":94,"sourceCode":"    ///   <para>\n    ///     When determining the closest value for a particular field, previously matched fields are used to filter the display mode list choices, and other fields are ignored.\n    ///     For example, when matching resolution, the display mode list will have already been filtered by a certain pixel format, while the refresh rate is ignored.\n    ///   </para>\n    ///   <para>\n    ///     This ordering doesn't define the absolute ordering for every usage scenario of <see cref=\"FindClosestMatchingDisplayMode\"/>, because the application can choose some\n    ///     values initially, effectively changing the order that fields are chosen. Fields of the display mode are matched one at a time, generally in a specified order.\n    ///     If a field is unspecified, this method gravitates toward the values for the desktop related to this output. If this output is not part of the desktop, then\n    ///     the default desktop output is used to find values.\n    ///   </para>\n    ///   <para>\n    ///     If an application uses a fully unspecified display mode, <see cref=\"FindClosestMatchingDisplayMode\"/> will typically return a display mode that matches the\n    ///     desktop settings for this output.\n    ///   </para>\n    /// </remarks>\n    public DisplayMode FindClosestMatchingDisplayMode(ReadOnlySpan<GraphicsProfile> targetProfiles, DisplayMode modeToMatch)\n    {\n        if (targetProfiles.IsEmpty)\n            throw new ArgumentNullException(nameof(targetProfiles));\n\n        var d3d12 = D3D12.GetApi();\n\n        // NOTE: Assume the same underlying integer type\n        Debug.Assert(sizeof(GraphicsProfile) == sizeof(D3DFeatureLevel));\n        var featureLevels = targetProfiles.As<GraphicsProfile, D3DFeatureLevel>();\n\n        HResult result = default;\n\n        var nativeAdapter = Adapter.NativeAdapter.AsIUnknown();\n        ComPtr<ID3D12Device> deviceTemp = null;\n\n        for (int i = 0; i < featureLevels.Length; i++)\n        {\n            var featureLevelToTry = featureLevels[i];\n\n            // Create Device D3D12 with feature Level based on profile\n            result = d3d12.CreateDevice(nativeAdapter, featureLevelToTry, out deviceTemp);","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/Direct3D12/GraphicsOutput.Direct3D12.cs#L76-L112","documentation":"FindClosestMatchingDisplayMode requires a non-empty set of target GraphicsProfiles to map onto D3D feature levels when querying the closest matching display mode. An empty span carries no profile information, so the library throws ArgumentNullException (named 'targetProfiles') immediately instead of issuing a meaningless D3D12 query.","triggerScenarios":"Calling GraphicsOutput.FindClosestMatchingDisplayMode with a default/empty ReadOnlySpan<GraphicsProfile> (e.g. Array.Empty<GraphicsProfile>(), stackalloc of length 0, or a filtered profile list that ended up empty).","commonSituations":"Building the profile list dynamically from device capabilities and a filter removing all entries; passing a span constructed from an empty collection; refactoring that changed GraphicsProfile[] to a span without re-checking length.","solutions":["Ensure at least one GraphicsProfile is in the span before calling, e.g. fall back to the device's current/minimum supported profile","If computing profiles dynamically, validate the list and throw/skip earlier with a clear message","Pass device.Features.Profile (or the highest supported profile) when the caller has no explicit preference"],"exampleFix":"// before\noutput.FindClosestMatchingDisplayMode(ReadOnlySpan<GraphicsProfile>.Empty, mode);\n// after\nvar profiles = new[] { GraphicsProfile.Level_10_0 };\noutput.FindClosestMatchingDisplayMode(profiles, mode);","handlingStrategy":"validation","validationCode":"if (profiles == null || profiles.Length == 0) throw new ArgumentException(\"At least one GraphicsProfile is required\", nameof(profiles));","typeGuard":"bool HasProfiles(ReadOnlySpan<GraphicsProfile> p) => !p.IsEmpty;","tryCatchPattern":"try { mode = output.FindClosestMatchingDisplayMode(profiles, mode); } catch (ArgumentNullException) { mode = output.CurrentDisplayMode; }","preventionTips":["Never pass Array.Empty<GraphicsProfile>() or a default span to display-mode queries","Always include at least one fallback profile (e.g. the device's supported minimum)"],"tags":["direct3d12","graphics","argument-validation"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}