{"record":{"id":"61057fe80b1d61a5","repo":"louthy/language-ext","slug":"argumentnullexception-lst-internal","errorCode":null,"errorMessage":"ArgumentNullException","messagePattern":"ArgumentNullException","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/List/Internal/Lst.Internal.cs","lineNumber":282,"sourceCode":"        if (index < 0 || index >= Root.Count) throw new IndexOutOfRangeException();\n        if (index + count > Root.Count) throw new IndexOutOfRangeException();\n\n        var self = this;\n        for (; count > 0; count--)\n        {\n            self = self.RemoveAt(index);\n        }\n        return self;\n    }\n\n    /// <summary>\n    /// Set an item at the specified index\n    /// </summary>\n    [Pure]\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public LstInternal<A> SetItem(int index, A value)\n    {\n        if (isnull(value)) throw new ArgumentNullException(nameof(value));\n        if (index < 0 || index >= Root.Count) throw new IndexOutOfRangeException();\n        return new LstInternal<A>(ListModule.SetItem(Root, value, index));\n    }\n\n    [Pure]\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    IEnumerator IEnumerable.GetEnumerator() =>\n        new ListEnumerator<A>(Root, false, 0);\n\n    [Pure]\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    IEnumerator<A> IEnumerable<A>.GetEnumerator() =>\n        new ListEnumerator<A>(Root, false, 0);\n\n    [Pure]\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public Iterable<A> Skip(int amount)\n    {","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/List/Internal/Lst.Internal.cs#L264-L300","documentation":"LstInternal.SetItem(int index, A value) throws ArgumentNullException when the replacement value is null. Lst (the AVL-backed persistent list) forbids null elements, so even a value-replacement at a valid index must be non-null; this check runs before the index check.","triggerScenarios":"Calling SetItem(index, null) — directly, or via a Func<A,A> updater that returns null (e.g. mapping an item to a null result) and then setting it back.","commonSituations":"Updater lambdas like x => x?.Child that can yield null; deserialized data containing nulls; nullable reference types disabled so the compiler does not warn about passing null.","solutions":["Ensure the replacement value is non-null before calling SetItem.","If the value may be null, wrap it in an Option<A> or a sentinel value rather than storing null.","Fix the Func<V,V>/updater so it never returns null (use Option.Match with a fallback).","Enable nullable reference types so null passing is caught at compile time."],"exampleFix":"// before\nvar updated = lst.SetItem(0, user?.Name); // null if user null\n// after\nvar name = user?.Name ?? throw new InvalidOperationException(\"user must have a name\");\nvar updated = lst.SetItem(0, name);","handlingStrategy":"validation","validationCode":"if (value is null)\n    throw new ArgumentNullException(nameof(value));\nvar updated = lst.SetItem(index, value);","typeGuard":"static bool IsSettable<A>(A value) where A : class => value is not null;","tryCatchPattern":"try { lst = lst.SetItem(index, value); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"value\")\n{\n    // handle null replacement value\n}","preventionTips":["Enable C# nullable reference types so null passing is a compile error.","Never allow updater functions (Func<A,A>) to return null; use Option<A>.","Sanitize deserialized data for nulls before loading it into Lst.","Use Option or a sentinel object instead of null elements."],"tags":["csharp","languageext","list","null-argument"],"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"}