{"record":{"id":"357480b71d669f39","repo":"TheAlgorithms/C-Sharp","slug":"value-cannot-be-null-parameter-adjacencymatrix","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'adjacencyMatrix')","messagePattern":"Value cannot be null\\. \\(Parameter 'adjacencyMatrix'\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Problems/GraphColoring/GraphColoringSolver.cs","lineNumber":71,"sourceCode":"    /// Thrown when the adjacency matrix is not square, when <paramref name=\"numColors\"/> is non-positive,\n    /// or when no valid coloring exists with the given number of colors.\n    /// </exception>\n    /// <remarks>\n    /// <para>\n    /// This method finds the first valid coloring it encounters. Multiple valid colorings\n    /// may exist for a given graph, but only one is returned.\n    /// </para>\n    /// <para>\n    /// <b>Example:</b> For a triangle graph (3 vertices, all connected), at least 3 colors\n    /// are required. Calling this method with <c>numColors = 2</c> will throw an exception,\n    /// while <c>numColors = 3</c> will return a valid coloring such as <c>[0, 1, 2]</c>.\n    /// </para>\n    /// </remarks>\n    public int[] ColorGraph(bool[,] adjacencyMatrix, int numColors)\n    {\n        if (adjacencyMatrix is null)\n        {\n            throw new ArgumentNullException(nameof(adjacencyMatrix));\n        }\n\n        var numVertices = adjacencyMatrix.GetLength(0);\n\n        if (numVertices != adjacencyMatrix.GetLength(1))\n        {\n            throw new ArgumentException(\"Adjacency matrix must be square.\", nameof(adjacencyMatrix));\n        }\n\n        if (numColors <= 0)\n        {\n            throw new ArgumentException(\"Number of colors must be positive.\", nameof(numColors));\n        }\n\n        // Handle empty graph\n        if (numVertices == 0)\n        {\n            return Array.Empty<int>();","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Problems/GraphColoring/GraphColoringSolver.cs#L53-L89","documentation":"GraphColoringSolver.ColorGraph throws ArgumentNullException when the adjacencyMatrix parameter is null. The solver needs the boolean adjacency matrix to determine vertex neighborhoods; without it no coloring can proceed.","triggerScenarios":"Calling ColorGraph(null, k) — e.g. a matrix built conditionally that was never assigned, or a method returning a nullable 2D array that is null on some code paths.","commonSituations":"Matrix deserialization from JSON/file failed silently and returned null; a factory method returned null for an empty/invalid graph spec; refactoring renamed a variable leaving the matrix unassigned.","solutions":["Ensure the adjacency matrix is constructed before calling ColorGraph.","Check for null at the call site and handle the empty/failed graph case explicitly.","Fix the upstream deserialization/factory path that returned null."],"exampleFix":"// before\nbool[,] matrix = LoadMatrix(path); // may be null\nvar colors = solver.ColorGraph(matrix, 3);\n// after\nbool[,] matrix = LoadMatrix(path) ?? new bool[0, 0];\nif (matrix.GetLength(0) == 0) return Array.Empty<int>();\nvar colors = solver.ColorGraph(matrix, 3);","handlingStrategy":"type-guard","validationCode":"if (adjacencyMatrix is null) throw new InvalidOperationException(\"Graph matrix not initialized.\");","typeGuard":"static bool IsValidMatrix(bool[,]? m) => m is not null && m.GetLength(0) == m.GetLength(1);","tryCatchPattern":"try { colors = solver.ColorGraph(matrix, k); }\ncatch (ArgumentNullException) { colors = Array.Empty<int>(); }","preventionTips":["Enable nullable reference types","Ensure loaders return empty matrices, never null","Guard factory results before use"],"tags":["null-argument","graph-coloring","argument-null"],"backgroundTag":"null-argument","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}