louthy/language-ext · error · NullReferenceException
NullReferenceException
Error message
NullReferenceException
What it means
Box.MakeNewClass builds (via IL emit, or as a fallback) a Func<A, object> that boxes a value; the fallback lambda ends in `?? throw new NullReferenceException()` at line 86, so passing a null A where A is a reference type throws NullReferenceException instead of silently producing a null box. Box is meant to wrap a value in a reference cell, so null input is treated as invalid. The error surfaces at Box construction time rather than later at dereference.
Solutions
- Check for null before constructing the Box and handle the null case explicitly
- Use LanguageExt's optionality types (Option<A>/OptionUnsafe) instead of Box for possibly-absent values
- Map null to a default/sentinel value before boxing
- Catch NullReferenceException only at generic boundaries where nullability cannot be constrained
Example fix
// before
string? s = dict.GetValueOrDefault("k");
var b = Box(s); // NullReferenceException when s is null
// after
string? s = dict.GetValueOrDefault("k");
var b = s is not null ? Box(s) : Box(""); // or use Option<string> Defensive patterns
Strategy: validation
Validate before calling
if (value is null) throw new ArgumentNullException(nameof(value)); var b = Box(value);
Type guard
static Box<A> TryBox<A>(A? x) where A : class => x is null ? throw new ArgumentNullException(nameof(x)) : Box(x);
Try / catch
try { var b = Box(maybeNull); }
catch (NullReferenceException) { b = null; /* absent value */ } Prevention
- Null-check values before boxing them
- Use Option<A> instead of Box for possibly-absent values
- Constrain generics to non-nullable where null is invalid
- Avoid boxing results of nullable-returning APIs without checks
When it happens
Trigger: Calling Box.Of/Box construction with a null value for a reference-type A (e.g. Box<string> with null, or a nullable class instance that is actually null); the delegate created by MakeNewClass throws immediately on null input.
Common situations: Wrapping results from nullable APIs (deserialization, dictionary lookups, optional returns) without a null check; generic code where T is unconstrained and happens to be a nullable reference type carrying null.
Related errors
- Ord attribute should have a struct type that derives from…
- Hashable attribute should have a struct type that derives…
- Don't use Equals - use either RecordType
- Don't use Equals - use either RecordType
- s
AI-assisted analysis of louthy/language-ext@2f0e362824 (2026-09-15).
Data as JSON: /api/errors/d43b59c197359dd8.
Report an issue: GitHub.
Appendix: source
Thrown at LanguageExt.Core/Utility/Box.cs:86
return (object x) => ((Box<A>)x).Value;
}
}
static Func<A, object> MakeNewClass()
{
if (ILCapability.Available)
{
var dynamic = new DynamicMethod("New_Class", typeof(object), new[] {typeof(A)}, typeof(A).Module, true);
var il = dynamic.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ret);
return (Func<A, object>)dynamic.CreateDelegate(typeof(Func<A, object>));
}
else
{
return static (A x) => (object?)x ?? throw new NullReferenceException();
}
}
static Func<A, object> MakeNewStruct()
{
if (ILCapability.Available)
{
var ctor = typeof(Box<A>).GetConstructor(new[] {typeof(A)});
var dynamic = new DynamicMethod("New_Struct", typeof(object), new[] {typeof(A)}, typeof(A).Module, true);
var il = dynamic.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Newobj, ctor!);
il.Emit(OpCodes.Ret);
return (Func<A, object>)dynamic.CreateDelegate(typeof(Func<A, object>));
}
elseView on GitHub (pinned to 2f0e362824)