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

  1. Check for null before constructing the Box and handle the null case explicitly
  2. Use LanguageExt's optionality types (Option<A>/OptionUnsafe) instead of Box for possibly-absent values
  3. Map null to a default/sentinel value before boxing
  4. 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

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


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>));
            }
            else

View on GitHub (pinned to 2f0e362824)