{"record":{"id":"d43b59c197359dd8","repo":"louthy/language-ext","slug":"nullreferenceexception","errorCode":null,"errorMessage":"NullReferenceException","messagePattern":"NullReferenceException","errorType":"exception","errorClass":"NullReferenceException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Utility/Box.cs","lineNumber":86,"sourceCode":"                return (object x) => ((Box<A>)x).Value;\n            }\n        }\n\n        static Func<A, object> MakeNewClass()\n        {\n            if (ILCapability.Available)\n            {\n                var dynamic = new DynamicMethod(\"New_Class\", typeof(object), new[] {typeof(A)}, typeof(A).Module, true);\n                var il      = dynamic.GetILGenerator();\n\n                il.Emit(OpCodes.Ldarg_0);\n                il.Emit(OpCodes.Ret);\n\n                return (Func<A, object>)dynamic.CreateDelegate(typeof(Func<A, object>));\n            }\n            else\n            {\n                return static (A x) => (object?)x ?? throw new NullReferenceException();\n            }\n        }\n\n        static Func<A, object> MakeNewStruct()\n        {\n            if (ILCapability.Available)\n            {\n                var ctor    = typeof(Box<A>).GetConstructor(new[] {typeof(A)});\n                var dynamic = new DynamicMethod(\"New_Struct\", typeof(object), new[] {typeof(A)}, typeof(A).Module, true);\n                var il      = dynamic.GetILGenerator();\n\n                il.Emit(OpCodes.Ldarg_0);\n                il.Emit(OpCodes.Newobj, ctor!);\n                il.Emit(OpCodes.Ret);\n\n                return (Func<A, object>)dynamic.CreateDelegate(typeof(Func<A, object>));\n            }\n            else","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Utility/Box.cs#L68-L104","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nstring? s = dict.GetValueOrDefault(\"k\");\nvar b = Box(s); // NullReferenceException when s is null\n// after\nstring? s = dict.GetValueOrDefault(\"k\");\nvar b = s is not null ? Box(s) : Box(\"\"); // or use Option<string>","handlingStrategy":"validation","validationCode":"if (value is null) throw new ArgumentNullException(nameof(value));\nvar b = Box(value);","typeGuard":"static Box<A> TryBox<A>(A? x) where A : class => x is null ? throw new ArgumentNullException(nameof(x)) : Box(x);","tryCatchPattern":"try { var b = Box(maybeNull); }\ncatch (NullReferenceException) { b = null; /* absent value */ }","preventionTips":["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"],"tags":["csharp","languageext","null","box"],"backgroundTag":"null-argument","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}