Blazor: Null checks and balances
web csharp blazor helpers
Recently I found my Blazor Server webs site running on Azure ran into some runtime null reference issues The site was unchanged from a month ago when when it ran OK and the backend database was unchanged. AI kept telling me the error/s must be in the code or in database. I feel something must have changed with underlying .NET libraries or Nuget/Telerik packages. ???
About
This a semi private site that enables athletic club club helpers to volunteer for tasks on competition day. It also maintains a list of club records with a formal submission and approval process.
First fixes
I let GitHub Copilot make 2 specific changes for two separate runtime errors and the app was back up and running π.
Errors seemed to be null or possibly null issues.
For example:
ProgramSchedules = await service.GetAVSLPrograms();
var x = from p in ProgramSchedules
where Settings.IsCurrentRegisterationPeriod(p.Date)
orderby p.dbCompetition, p.Date, p.Round, p.LocationId
select p;
ProgramSchedules = x.ToList();
It was failing on the last line.
The recommendation was to change:
orderby p.dbCompetition, p.Date, p.Round, p.LocationId
to β¦
orderby (p.dbCompetition?.Name ?? ""), p.Date, p.Round, p.LocationId
There were a few locations in the code like this.
Another change was to change:
orderby p.dbCompetition, p.Date, p.Round, p.LocationId
to..
orderby (p.dbCompetition?.Name ?? "")
A build and local run checks were performed and it passed. I then did a deploy check and that worked. The repository was then committed to.
Digging deeper
I then let AI scan the whole project, which has been developed and extended over several years, consists of numerous class and razor files. It came up with 6 areas of action. A document was generated before the actions were performed:
π― Harden DB-related code: top-priority fixes
Understanding
Apply six high-priority defensive changes to prevent NullReferenceException and ordering errors in DB-facing code. Make one change at a time, build the solution after each change, and prompt the user to run tests before moving to the next change.
Assumptions
- The workspace is the provided repository; builds succeed locally.
- Changes should be minimal and limited to the specific high-risk patterns identified earlier.
Approach
Make six sequential, atomic edits. After each edit: run a build, report results, call update_plan_progress for that step, then ask the user whether to run tests and whether to continue to the next step.
Key Files
- Pages/FetchActivitys.razor - ordering by dbCompetition
- Pages/FetchDateActivitys.razor, Pages/Newactivity.razor, Pages/AVSLProgramEdit.razor - ordering fixes already applied but will re-scan
- Data/DataAccessService.cs - guards around l.Round.Id and FirstOrDefault uses
- Pages/SelectHelper.razor - guard activity.Round accesses
- Data/EmailMsg.cs, Data/TwilloSendSMSService.cs - guard email/name usages
Risks & Open Questions
- Some fixes may change UI ordering subtly (sorting by Name vs object). Confirm acceptable.
- Tests may depend on behaviors; will prompt before running tests.
π Plan Steps
- π step-1: Replace OrderBy/orderby usages that sort by dbCompetition object with a comparable key (dbCompetition?.Name ?? ββ) β change FetchActivitys.razor (2 sites).
- β step-2: Add null guards before l.Round.Id or p.Round.Date usages in DataAccessService methods (GetRoundActivitys, other LINQ filters) to prevent NREs.
- β step-3: Add explicit null-checks after FirstOrDefault usages in DataAccessService where code immediately accesses properties (e.g., ToggleAVSLProgram, DuplicateActivity) and return gracefully.
- β step-4: Add null-check guards in Pages that access activity.Round.Id without confirming non-null (SelectHelper.razor and other pages), returning or handling null case.
- π step-5: Add guards around MailAddress and email usage in Data/EmailMsg.cs and TwilloSendSMSService.cs to skip or log when email or recipient is null/empty.
- step-6: Run a repo grep to find any remaining orderby/OrderBy usages against dbCompetition or other objects; patch remaining occurrences.
Outcome
The 6 steps were performed by AI separately with a build and run test between each. All is OK now.
These updates should make the siteβs functionality more resilient.
| Topic | Subtopic | |
| < Prev: | Athletics Certificate Generator | More generalised Generators |
| This Category Links | ||
| Category: | Web Sites Index: | Web Sites |