{"record":{"id":"338ed532eec222f8","repo":"TheAlgorithms/C-Sharp","slug":"the-parameters-listofas-and-listofns-must-not-be-null-and","errorCode":null,"errorMessage":"The parameters 'listOfAs' and 'listOfNs' must not be null and have to be of equal length!","messagePattern":"The parameters 'listOfAs' and 'listOfNs' must not be null and have to be of equal length!","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/ModularArithmetic/ChineseRemainderTheorem.cs","lineNumber":124,"sourceCode":"        if (result < 0)\n        {\n            result += prodN;\n        }\n\n        return result;\n    }\n\n    /// <summary>\n    /// Checks the requirements for the algorithm and throws an ArgumentException if they are not being met.\n    /// </summary>\n    /// <param name=\"listOfAs\">An ordered list of a_0, a_1, ..., a_k.</param>\n    /// <param name=\"listOfNs\">An ordered list of n_0, n_1, ..., n_k.</param>\n    /// <exception cref=\"ArgumentException\">If any of the requirements is not fulfilled.</exception>\n    private static void CheckRequirements(List<long> listOfAs, List<long> listOfNs)\n    {\n        if (listOfAs == null || listOfNs == null || listOfAs.Count != listOfNs.Count)\n        {\n            throw new ArgumentException(\"The parameters 'listOfAs' and 'listOfNs' must not be null and have to be of equal length!\");\n        }\n\n        if (listOfNs.Any(x => x <= 1))\n        {\n            throw new ArgumentException($\"The value {listOfNs.First(x => x <= 1)} for some n_i is smaller than or equal to 1.\");\n        }\n\n        if (listOfAs.Any(x => x < 0))\n        {\n            throw new ArgumentException($\"The value {listOfAs.First(x => x < 0)} for some a_i is smaller than 0.\");\n        }\n\n        // Check if all pairs of (n_i, n_j) are coprime:\n        for (var i = 0; i < listOfNs.Count; i++)\n        {\n            for (var j = i + 1; j < listOfNs.Count; j++)\n            {\n                long gcd;","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/ModularArithmetic/ChineseRemainderTheorem.cs#L106-L142","documentation":"ChineseRemainderTheorem.CheckRequirements (long overload) validates its inputs before solving the congruence system. It throws ArgumentException when either list is null or when the lists of remainders (a_i) and moduli (n_i) have different lengths, since each a_i must pair with one n_i.","triggerScenarios":"Calling Compute with a null List<long> for listOfAs or listOfNs, or lists of unequal counts (e.g. 3 remainders but 2 moduli).","commonSituations":"Parsing congruence systems from text where one line failed to parse and was skipped; building the lists in separate loops with different conditions; passing uninitialized lists.","solutions":["Ensure every remainder a_i has a corresponding modulus n_i so both lists have equal, non-null length.","Null-check or default-initialize both lists before calling Compute.","Build both lists in a single pass over the parsed input so they cannot diverge."],"exampleFix":"// before\nChineseRemainderTheorem.Compute(new List<long> { 2, 3 }, null); // throws\n\n// after\nif (listOfAs != null && listOfNs != null && listOfAs.Count == listOfNs.Count)\n{\n    ChineseRemainderTheorem.Compute(listOfAs, listOfNs);\n}","handlingStrategy":"validation","validationCode":"if (listOfAs == null || listOfNs == null || listOfAs.Count != listOfNs.Count)\n{\n    throw new ArgumentException(\"Remainders and moduli must be non-null and of equal length.\");\n}\nChineseRemainderTheorem.Compute(listOfAs, listOfNs);","typeGuard":"static bool IsPairedInput<T>(List<T> a, List<T> n) => a != null && n != null && a.Count == n.Count;","tryCatchPattern":"try\n{\n    var result = ChineseRemainderTheorem.Compute(listOfAs, listOfNs);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"must not be null and have to be of equal length\"))\n{\n    logger.LogError(\"CRT input lists unpaired: a={A}, n={N}\", listOfAs?.Count, listOfNs?.Count);\n    throw;\n}","preventionTips":["Parse each congruence line into a single (a, n) pair so the lists cannot diverge.","Null-check inputs at the API boundary before any math code.","Add tests for null and unequal-length inputs."],"tags":["math","modular-arithmetic","null-argument","csharp"],"backgroundTag":"null-argument","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}