{"record":{"id":"3584424cd7bb2614","repo":"MonoGame/MonoGame","slug":"numvertices","errorCode":null,"errorMessage":"numVertices","messagePattern":"numVertices","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/Graphics/GraphicsDevice.cs","lineNumber":1396,"sourceCode":"        /// <param name=\"numVertices\">The number of vertices to draw.</param>\r\n        /// <param name=\"indexData\">The index data.</param>\r\n        /// <param name=\"vertexDeclaration\">The layout of the vertices.</param>\r\n        /// <remarks>All indices in the vertex buffer are interpreted relative to the specified <paramref name=\"vertexOffset\"/>.\r\n        /// For example a value of zero in the array of indices points to the vertex at index <paramref name=\"vertexOffset\"/>\r\n        /// in the array of vertices.</remarks>\r\n        public void DrawUserIndexedPrimitives<T>(PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, short[] indexData, int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration) where T : struct\r\n        {\r\n            // These parameter checks are a duplicate of the checks in the int[] overload of DrawUserIndexedPrimitives.\r\n            // Inlined here for efficiency.\r\n\r\n            if (vertexData == null || vertexData.Length == 0)\r\n                throw new ArgumentNullException(\"vertexData\");\r\n\r\n            if (vertexOffset < 0 || vertexOffset >= vertexData.Length)\r\n                throw new ArgumentOutOfRangeException(\"vertexOffset\");\r\n\r\n            if (numVertices <= 0 || numVertices > vertexData.Length)\r\n                throw new ArgumentOutOfRangeException(\"numVertices\");\r\n\r\n            if (vertexOffset + numVertices > vertexData.Length)\r\n                throw new ArgumentOutOfRangeException(\"numVertices\");\r\n\r\n            if (indexData == null || indexData.Length == 0)\r\n                throw new ArgumentNullException(\"indexData\");\r\n\r\n            if (indexOffset < 0 || indexOffset >= indexData.Length)\r\n                throw new ArgumentOutOfRangeException(\"indexOffset\");\r\n\r\n            if (primitiveCount <= 0)\r\n                throw new ArgumentOutOfRangeException(\"primitiveCount\");\r\n\r\n            if (indexOffset + GetElementCountArray(primitiveType, primitiveCount) > indexData.Length)\r\n                throw new ArgumentOutOfRangeException(\"primitiveCount\");\r\n\r\n            if (vertexDeclaration == null)\r\n                throw new ArgumentNullException(\"vertexDeclaration\");\r","sourceCodeStart":1378,"sourceCodeEnd":1414,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/Graphics/GraphicsDevice.cs#L1378-L1414","documentation":"Thrown by DrawUserIndexedPrimitives<T> when numVertices is <= 0 or greater than vertexData.Length. numVertices declares how many vertices (from vertexOffset) are available for indexing; it must be a positive count that fits within the array. This check runs after the vertexOffset range check.","triggerScenarios":"Calling DrawUserIndexedPrimitives with numVertices <= 0, or numVertices > vertexData.Length.","commonSituations":"Passing vertexData.Length as numVertices after the array shrank (stale count). Passing 0 because the active vertex count was not yet computed. Confusing numVertices (a count) with the last vertex index. A sub-range draw where numVertices was sized for the whole buffer but vertexOffset > 0.","solutions":["Set numVertices to the live count of vertices used by the indices: usually vertexData.Length for a full-array draw, or a tracked active count for a subrange.","Ensure 0 < numVertices <= vertexData.Length, and additionally vertexOffset + numVertices <= vertexData.Length.","Recompute numVertices whenever the vertex array or the active range changes."],"exampleFix":"// before\n_graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, _verts, 0, _cachedVertCount, _indices, 0, primCount, decl);\n\n// after\nint numVertices = Math.Min(_cachedVertCount, _verts.Length);\nif (numVertices > 0)\n    _graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, _verts, 0, numVertices, _indices, 0, primCount, decl);","handlingStrategy":"validation","validationCode":"int numVertices = Math.Clamp(requestedNumVertices, 1, vertexData.Length);\nif (numVertices <= 0) return;","typeGuard":"static bool IsValidNumVertices<T>(T[] vertexData, int numVertices) where T : struct\n    => vertexData != null && numVertices > 0 && numVertices <= vertexData.Length;","tryCatchPattern":"try { _graphicsDevice.DrawUserIndexedPrimitives(pt, vertexData, vertexOffset, numVertices, indexData, indexOffset, primCount, decl); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"numVertices\")\n{\n    // numVertices <= 0 or > vertexData.Length; recompute.\n}","preventionTips":["Set numVertices from the live vertex count, not a cached full-buffer length.","Ensure 0 < numVertices <= vertexData.Length.","Recompute numVertices whenever the vertex array or active range changes.","Distinguish numVertices (a count) from the last vertex index."],"tags":["monogame","graphics","drawing","argument-validation","user-indexed-primitives"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}