builtbybel/FlyOOBE · error · ArgumentNullException
null
Error message
null
What it means
The Logger constructor validates that a MainForm instance is provided, since every log entry must be rendered onto that form's UI (it calls mainForm.InvokeRequired/Invoke). If null is passed, it throws ArgumentNullException immediately rather than failing later on the first log call.
Solutions
- Pass a valid, already-constructed MainForm instance to the Logger constructor
- Ensure MainForm is created before Logger (reorder startup code)
- If the form may not exist yet, defer Logger creation until after form construction, or make mainForm nullable and buffer logs
- In tests, pass a stub MainForm or refactor Logger to accept an abstraction (e.g. Action<string> sink)
Example fix
// before var logger = new Logger(null); // after MainForm mainForm = new MainForm(); var logger = new Logger(mainForm);
Defensive patterns
Strategy: validation
Validate before calling
if (mainForm == null)
throw new InvalidOperationException("MainForm must be constructed before creating a Logger");
var logger = new Logger(mainForm); Type guard
bool IsValidLoggerTarget(MainForm form) => form != null && !form.IsDisposed;
Try / catch
try
{
var logger = new Logger(mainForm);
}
catch (ArgumentNullException ex)
{
// ex.ParamName == "mainForm"
LogFallback("Logger init failed: main form reference was null");
} Prevention
- Construct MainForm before any component that depends on it
- Check form references for null/IsDisposed before wiring loggers
- Prefer constructor injection with a DI container so null dependencies fail at composition time
- Unit-test Logger construction with a stub form
When it happens
Trigger: Calling `new Logger(null)` — e.g. before the MainForm instance is constructed, passing an uninitialized form field, or constructing a Logger in a static context where the form reference is not yet assigned.
Common situations: Refactoring the app startup so the Logger is created before InitializeComponent of the main form; passing a different Form type that is null; unit-testing Logger without a form instance; dependency-injection container not registered for MainForm.
Related errors
AI-assisted analysis of builtbybel/FlyOOBE@ed093a784d (2026-09-14).
Data as JSON: /api/errors/a5d851412bfe8c58.
Report an issue: GitHub.
Appendix: source
Thrown at Flyby11-deprecated/Flyby11/Logger.cs:14
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace Flyby11
{
public class Logger
{
private MainForm mainForm;
public Logger(MainForm mainForm)
{
this.mainForm = mainForm ?? throw new ArgumentNullException(nameof(mainForm));
}
// Log method for a single string
public void Log(string message, Color color, float fontSize = 10.5f)
{
if (mainForm.InvokeRequired)
{
mainForm.Invoke(new Action(() => Log(message, color, fontSize)));
return;
}
AppendMessageToConversation(message, color, fontSize); // Append message to conversation
}
private void AppendMessageToConversation(string message, Color color, float fontSize)
{
Label statusLabel = mainForm.Controls.Find("statusLabel", true).FirstOrDefault() as Label;View on GitHub (pinned to ed093a784d)