Skip to content
Latchkey

C# "CS8602: Dereference of a possibly null reference" in CI

With nullable reference types enabled, CS8602 warns that you dereference a value the compiler cannot prove is non-null. It only fails the build when warnings are treated as errors (common in CI), so it often passes locally and breaks the pipeline.

What this error means

Build fails (or warns) with CS8602 at a member access on a maybe-null value. It is deterministic, and turns fatal under TreatWarningsAsErrors or <Nullable>enable</Nullable> with strict CI settings.

dotnet
User.cs(27,16): error CS8602: Dereference of a possibly null reference.

Diagnose it: SDK version and restore first

Most .NET CI failures are an SDK mismatch or a restore that did not happen. global.json pins the SDK, and if the runner does not have that exact version the failure message is about the project rather than the SDK.

Terminal
dotnet --info
cat global.json 2>/dev/null

# restore explicitly so a restore failure is not reported as a build failure
dotnet restore --verbosity normal
dotnet build --no-restore -warnaserror

Common causes

A nullable value is dereferenced without a check

The compiler's flow analysis cannot prove the value is non-null at the access point, so it warns.

Warnings are treated as errors in CI

A CS8602 that is merely a warning locally becomes fatal when CI promotes warnings to errors.

How to fix it

Guard or null-check before dereferencing

  1. Add a null check, pattern match, or early return before the access.
  2. Use the null-conditional operator where appropriate.
  3. Rebuild.
.cs
if (user is null) return NotFound();
return Ok(user.Name);

Express non-null intent precisely

  1. Use the null-forgiving operator ! only where you can guarantee non-null.
  2. Annotate APIs with proper nullability so flow analysis is accurate.
  3. Rebuild.

How to prevent it

  • Enable <Nullable>enable</Nullable> early so issues surface during development.
  • Fix null warnings rather than suppressing them broadly.
  • Run a strict build locally that mirrors CI warnings-as-errors.

Frequently asked questions

What causes C# "CS8602: dereference of a possibly null reference" in CI?
There are 2 common causes: a nullable value is dereferenced without a check and warnings are treated as errors in ci. The compiler's flow analysis cannot prove the value is non-null at the access point, so it warns.
How do I fix C# "CS8602: dereference of a possibly null reference" in CI?
There are 2 fixes depending on which cause you have: guard or null-check before dereferencing and express non-null intent precisely. Work through them in order, since the first is the most common.
What does C# "CS8602: dereference of a possibly null reference" in CI actually mean?
Build fails (or warns) with CS8602 at a member access on a maybe-null value.
How do I stop C# "CS8602: dereference of a possibly null reference" in CI happening again?
Enable <Nullable>enable</Nullable> early so issues surface during development. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card