TheAlgorithms/C-Sharp · error · ArgumentException

x has no value

Error message

x has no value

What it means

FibonacciHeap.DecreaseKey throws ArgumentException "x has no value" when the node's Key is null. DecreaseKey must compare the new value k against the existing key, which is impossible when the stored key is null.

Solutions

  1. Ensure the node was Push()ed onto the heap so its Key is assigned.
  2. Check x.Key != null before calling DecreaseKey().
  3. Avoid reusing node objects after they are popped.

Example fix

// before
heap.DecreaseKey(node, k); // node.Key == null
// after
if (node.Key != null)
{
    heap.DecreaseKey(node, k);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (node.Key != null) { heap.DecreaseKey(node, k); }

Type guard

bool HasKey(FHeapNode<T> node) => node.Key is not null;

Try / catch

try { heap.DecreaseKey(node, k); } catch (ArgumentException) { /* node key null / not in heap */ }

Prevention

When it happens

Trigger: DecreaseKey on a node whose Key property is null (e.g. a detached or default-constructed FHeapNode<T> with a nullable/uninitialized key).

Common situations: Using reference-type T where nodes were created without assigning Key, or reusing nodes after removal that reset their key.

Related errors


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

Appendix: source

Thrown at DataStructures/Heap/FibonacciHeap/FibonacciHeap.cs:232

    /// <summary>
    ///     Reduce the key of x to be k.
    /// </summary>
    /// <remarks>
    ///     k must be less than x.Key, increasing the key of an item is not supported.
    /// </remarks>
    /// <param name="x">The item you want to reduce in value.</param>
    /// <param name="k">The new value for the item.</param>
    public void DecreaseKey(FHeapNode<T> x, T k)
    {
        if (MinItem == null)
        {
            throw new ArgumentException($"{nameof(x)} is not from the heap");
        }

        if (x.Key == null)
        {
            throw new ArgumentException("x has no value");
        }

        if (k.CompareTo(x.Key) > 0)
        {
            throw new InvalidOperationException("Value cannot be increased");
        }

        x.Key = k;
        var y = x.Parent;
        if (y != null && x.Key.CompareTo(y.Key) < 0)
        {
            Cut(x, y);
            CascadingCut(y);
        }

        if (x.Key.CompareTo(MinItem.Key) < 0)
        {
            MinItem = x;

View on GitHub (pinned to 96e2905cab)