QuestPDF/QuestPDF · error · ArgumentNullException

The gradient colors cannot be null.

Error message

The gradient colors cannot be null.

What it means

Thrown by LineDescriptor.LineGradient when the colors argument is null. A gradient needs at least two color stops to interpolate, so a null Color[] cannot be applied and is rejected with an ArgumentNullException before the length check.

Source

Thrown at src/dotnet/library/QuestPDF/Fluent/LineExtensions.cs:53

                throw new ArgumentNullException(nameof(dashPattern), "The dash pattern cannot be null.");
            
            if (dashPattern.Length == 0)
                throw new ArgumentException("The dash pattern cannot be empty.", nameof(dashPattern));
            
            if (dashPattern.Length % 2 != 0)
                throw new ArgumentException("The dash pattern must contain an even number of elements.", nameof(dashPattern));
            
            Line.DashPattern = dashPattern.Select(x => x.ToPoints(unit)).ToArray();
            return this;
        }

        /// <summary>
        /// Applies a linear gradient to a line using the specified colors.
        /// </summary>
        public LineDescriptor LineGradient(Color[] colors)
        {
            if (colors == null)
                throw new ArgumentNullException(nameof(colors), "The gradient colors cannot be null.");

            if (colors.Length == 0)
                throw new ArgumentException("The gradient colors cannot be empty.", nameof(colors));

            Line.GradientColors = colors;
            return this;
        }
    }
    
    public static class LineExtensions
    {
        private static LineDescriptor Line(this IContainer element, LineType type, float thickness)
        {
            if (thickness < 0)
                throw new ArgumentOutOfRangeException(nameof(thickness), "The Line thickness cannot be negative.");
            
            var descriptor = new LineDescriptor();
            descriptor.Line.Thickness = thickness;

View on GitHub (pinned to 43ab125596)

Solutions

  1. Pass a non-empty Color[], e.g. LineGradient(new[] { Colors.Blue, Colors.Red }).
  2. Coalesce from a default palette: colors ?? Theme.DefaultGradient.
  3. Make the palette accessor return an empty/default array instead of null.

Example fix

// before
.Line(l => l.LineGradient(palette)); // palette is null

// after
.Line(l => l.LineGradient(palette ?? new[] { Colors.Grey.Lighten2, Colors.Grey.Darken2 }));
Defensive patterns

Strategy: validation

Validate before calling

colors ??= new[] { Colors.Grey.Lighten2, Colors.Grey.Darken2 };
line.LineGradient(colors);

Type guard

static bool HasGradientColors(Color[]? c) => c is { Length: > 0 };

Prevention

When it happens

Trigger: Calling .LineGradient(null) or passing a colors array that was never populated (e.g. a palette lookup that returned null). Guard: if (colors == null).

Common situations: Theme/palette service returns null when a key is missing; JSON config binds an optional colors array to null; conditional palette construction skipped the assignment branch.

Related errors


AI-assisted analysis of QuestPDF/QuestPDF@43ab125596 (2026-08-13). Data as JSON: /api/errors/062bd4a89c6b3ae5. Report an issue: GitHub.