C#: NullReferenceException
Attempting to access a member on a type that is null.
Quick fix
Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.
# Check for null
if (obj != null) { obj.Method(); }
# Use null-conditional operator
obj?.Method();
# Enable nullable reference types in .csproj
<Nullable>enable</Nullable>
How to diagnose C# errors
C# errors divide into compile-time and restore-time problems (NuGet resolution, target framework mismatches) and runtime problems dominated by null references and async misuse. The classic .NET production hazard is the sync-over-async deadlock, calling .Result or .Wait() on a task in a context with a synchronisation context, which manifests as a hang rather than an exception and is therefore much harder to spot.
If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.
- Run
dotnet restore --verbosity detailedto see which feed each package resolved from. Most restore failures are a private feed with expired credentials, not a missing package. - Enable nullable reference types (
<Nullable>enable</Nullable>) and treat the warnings as errors. This converts a whole class of production NullReferenceExceptions into compile failures. - For async hangs, search the codebase for
.Result,.Wait()and.GetAwaiter().GetResult(). Replace withawaitall the way up, or useConfigureAwait(false)in library code. - For EF Core, run
dotnet ef migrations listto see which migrations the database believes are applied before generating a new one. - Capture a dump with
dotnet-dumpand inspect it withdotnet-gcdump/dotnet-counterswhen the process misbehaves without throwing.
Tools worth reaching for
dotnet restore -v detaileddotnet ef migrations listdotnet-countersdotnet-dump
Authoritative references
Primary documentation for this error, worth reading before applying any fix in production.
Related C# errors
- .NET: NuGet restore failedNuGet package restore failed. Network issues, invalid package source, or missing credentials.
- .NET: The framework 'Microsoft.NETCore.App', version '8.0.0' was not foundThe published application is framework dependent and the machine has no matching runtime…
- ASP.NET Core: Unable to resolve service for type while activatingThe container was asked for a dependency nobody registered, so the controller or hosted…
- ASP.NET: Connection string not found in configurationThe application cannot find the database connection string in appsettings.json or environment…
- Blazor: JavaScript interop call failedA Blazor JS interop call threw an error because the JavaScript function was not found, threw…
- C#: CS8618 non-nullable property must contain a non-null value when exiting constructorWith nullable reference types enabled, the compiler requires every non-nullable member to be…
- C#: Deadlock in async/awaitCalling .Result or .Wait() on an async method from a synchronisation context (UI thread…
- EF Core: another instance with the same key value is already being trackedThe change tracker already holds an entity with that primary key and Attach or Update tried…
Browse other categories
- HTTP 494xx client errors, 5xx server errors, redirects, headers and protocol problems.
- JavaScript 42npm resolution, async pitfalls, hydration, memory limits and runtime type…
- Database 41Connections, deadlocks, constraints, replication and memory limits.
- AI 35Rate limits, context windows, GPU memory and model-serving failures.
- Network 35Refused connections, timeouts, resets, MTU problems and port exhaustion.
- Python 35Imports, virtual environments, encoding, concurrency and dependency conflicts.
- Kubernetes 34CrashLoopBackOff, ImagePullBackOff, OOMKilled, RBAC, scheduling and storage.
- Docker 27Daemon connectivity, disk space, image pulls, ports and architecture mismatches.
- System 26Disk space, systemd units, file descriptors, OOM killer and scheduled jobs.
- Cloud 25IAM permissions, quotas, service limits and credential failures.
- Security 25JWT validation, CSRF, OAuth grants, SELinux, SSH host keys and CSP.
- TLS 24Untrusted authorities, expiry, hostname mismatch, chains and cipher negotiation.
Something missing or wrong?
This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.