TheAlgorithms/C-Sharp · error

Value cannot be null. (Parameter 'jobs')

Error message

Value cannot be null. (Parameter 'jobs')

What it means

IntervalSchedulingSolver.Schedule throws ArgumentNullException when the jobs parameter is null. The greedy earliest-finish-first algorithm needs the job collection to select a maximal non-overlapping set.

Solutions

  1. Pass an empty collection instead of null: Schedule(Enumerable.Empty<Job>()).
  2. Coalesce at the call site: jobs ?? Enumerable.Empty<Job>().
  3. Fix the data-loading path that returned null rather than an empty sequence.

Example fix

// before
List<Job> jobs = LoadJobs(); // may be null
var selected = IntervalSchedulingSolver.Schedule(jobs);
// after
var selected = IntervalSchedulingSolver.Schedule(LoadJobs() ?? Enumerable.Empty<Job>());
Defensive patterns

Strategy: type-guard

Validate before calling

if (jobs is null) jobs = Enumerable.Empty<Job>();

Type guard

static bool HasJobs(IEnumerable<Job>? jobs) => jobs != null;

Try / catch

try { selected = IntervalSchedulingSolver.Schedule(jobs); }
catch (ArgumentNullException) { selected = new List<Job>(); }

Prevention

When it happens

Trigger: Calling Schedule(null), or passing a nullable IEnumerable<Job> that is null when no jobs were loaded.

Common situations: A repository/query returned null instead of an empty list; optional job input not initialized before scheduling; LINQ FirstOrDefault-style call returning null.

Related errors


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

Appendix: source

Thrown at Algorithms/Problems/JobScheduling/IntervalSchedulingSolver.cs:22

namespace Algorithms.Problems.JobScheduling;

/// <summary>
/// Implements the greedy algorithm for Interval Scheduling.
/// Finds the maximum set of non-overlapping jobs.
/// </summary>
public static class IntervalSchedulingSolver
{
    /// <summary>
    /// Returns the maximal set of non-overlapping jobs.
    /// </summary>
    /// <param name="jobs">List of jobs to schedule.</param>
    /// <returns>List of selected jobs (maximal set).</returns>
    public static List<Job> Schedule(IEnumerable<Job> jobs)
    {
        if (jobs == null)
        {
            throw new ArgumentNullException(nameof(jobs));
        }

        // Sort jobs by their end time (earliest finish first)
        var sortedJobs = jobs.OrderBy(j => j.End).ToList();
        var result = new List<Job>();
        int lastEnd = int.MinValue;

        foreach (var job in sortedJobs)
        {
            // If the job starts after the last selected job ends, select it
            if (job.Start >= lastEnd)
            {
                result.Add(job);
                lastEnd = job.End;
            }
        }

        return result;

View on GitHub (pinned to 96e2905cab)