TheAlgorithms/C-Sharp · error

Value cannot be null. (Parameter 'adjacencyMatrix')

Error message

Value cannot be null. (Parameter 'adjacencyMatrix')

What it means

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.

Solutions

  1. Ensure the adjacency matrix is constructed before calling ColorGraph.
  2. Check for null at the call site and handle the empty/failed graph case explicitly.
  3. Fix the upstream deserialization/factory path that returned null.

Example fix

// before
bool[,] matrix = LoadMatrix(path); // may be null
var colors = solver.ColorGraph(matrix, 3);
// after
bool[,] matrix = LoadMatrix(path) ?? new bool[0, 0];
if (matrix.GetLength(0) == 0) return Array.Empty<int>();
var colors = solver.ColorGraph(matrix, 3);
Defensive patterns

Strategy: type-guard

Validate before calling

if (adjacencyMatrix is null) throw new InvalidOperationException("Graph matrix not initialized.");

Type guard

static bool IsValidMatrix(bool[,]? m) => m is not null && m.GetLength(0) == m.GetLength(1);

Try / catch

try { colors = solver.ColorGraph(matrix, k); }
catch (ArgumentNullException) { colors = Array.Empty<int>(); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/357480b71d669f39. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Problems/GraphColoring/GraphColoringSolver.cs:71

    /// Thrown when the adjacency matrix is not square, when <paramref name="numColors"/> is non-positive,
    /// or when no valid coloring exists with the given number of colors.
    /// </exception>
    /// <remarks>
    /// <para>
    /// This method finds the first valid coloring it encounters. Multiple valid colorings
    /// may exist for a given graph, but only one is returned.
    /// </para>
    /// <para>
    /// <b>Example:</b> For a triangle graph (3 vertices, all connected), at least 3 colors
    /// are required. Calling this method with <c>numColors = 2</c> will throw an exception,
    /// while <c>numColors = 3</c> will return a valid coloring such as <c>[0, 1, 2]</c>.
    /// </para>
    /// </remarks>
    public int[] ColorGraph(bool[,] adjacencyMatrix, int numColors)
    {
        if (adjacencyMatrix is null)
        {
            throw new ArgumentNullException(nameof(adjacencyMatrix));
        }

        var numVertices = adjacencyMatrix.GetLength(0);

        if (numVertices != adjacencyMatrix.GetLength(1))
        {
            throw new ArgumentException("Adjacency matrix must be square.", nameof(adjacencyMatrix));
        }

        if (numColors <= 0)
        {
            throw new ArgumentException("Number of colors must be positive.", nameof(numColors));
        }

        // Handle empty graph
        if (numVertices == 0)
        {
            return Array.Empty<int>();

View on GitHub (pinned to 96e2905cab)