{"record":{"id":"91ac2d266f3c6e0f","repo":"nopSolutions/nopCommerce","slug":"email-cannot-be-null","errorCode":null,"errorMessage":"Email cannot be null","messagePattern":"Email cannot be null","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"error","filePath":"src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs","lineNumber":502,"sourceCode":"        if (!string.IsNullOrEmpty(returnUrl) && _webHelper.CheckIsLocalUrl(returnUrl))\r\n            return new RedirectResult(returnUrl);\r\n\r\n        return new RedirectToRouteResult(NopRouteNames.General.HOMEPAGE, null);\r\n    }\r\n\r\n    /// <summary>\r\n    /// Sets a user email\r\n    /// </summary>\r\n    /// <param name=\"customer\">Customer</param>\r\n    /// <param name=\"newEmail\">New email</param>\r\n    /// <param name=\"requireValidation\">Require validation of new email address</param>\r\n    /// <returns>A task that represents the asynchronous operation</returns>\r\n    public virtual async Task SetEmailAsync(Customer customer, string newEmail, bool requireValidation)\r\n    {\r\n        ArgumentNullException.ThrowIfNull(customer);\r\n\r\n        if (newEmail == null)\r\n            throw new NopException(\"Email cannot be null\");\r\n\r\n        newEmail = newEmail.Trim();\r\n        var oldEmail = customer.Email;\r\n\r\n        if (!CommonHelper.IsValidEmail(newEmail))\r\n            throw new NopException(await _localizationService.GetResourceAsync(\"Account.EmailUsernameErrors.NewEmailIsNotValid\"));\r\n\r\n        if (newEmail.Length > 100)\r\n            throw new NopException(await _localizationService.GetResourceAsync(\"Account.EmailUsernameErrors.EmailTooLong\"));\r\n\r\n        var customer2 = await _customerService.GetCustomerByEmailAsync(newEmail);\r\n        if (customer2 != null && customer.Id != customer2.Id)\r\n            throw new NopException(await _localizationService.GetResourceAsync(\"Account.EmailUsernameErrors.EmailAlreadyExists\"));\r\n\r\n        if (requireValidation)\r\n        {\r\n            //re-validate email\r\n            customer.EmailToRevalidate = newEmail;\r","sourceCodeStart":484,"sourceCodeEnd":520,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs#L484-L520","documentation":"Thrown by SetEmailAsync as a NopException when the newEmail argument is exactly null. This is a defensive check after the explicit ArgumentNullException.ThrowIfNull(customer) guard; it distinguishes a null email (programmer error) from an invalid one (user error).","triggerScenarios":"Calling await customerRegistrationService.SetEmailAsync(customer, null, requireValidation) — passing a literal null or a variable that was never assigned. Distinct from an empty/invalid string which hits a different branch.","commonSituations":"A controller action binding a nullable email field from a form/query and forwarding it without null-coalescing; deserialization of a customer-import payload where the email node was omitted; a refactor that changed the caller to read from an optional setting.","solutions":["Ensure the caller never passes null: coalesce to empty or validate presence before calling SetEmailAsync.","Make the source field non-nullable and required at the API boundary (e.g., [Required] on the request model).","If null is legitimately possible, short-circuit: if (newEmail is null) return; or surface a proper validation message."],"exampleFix":"// before\nawait _customerRegistrationService.SetEmailAsync(customer, model.Email, requireValidation: true);\n\n// after\nif (model.Email is null)\n    ModelState.AddModelError(nameof(model.Email), \"Email is required.\");\nelse\n    await _customerRegistrationService.SetEmailAsync(customer, model.Email, requireValidation: true);","handlingStrategy":"validation","validationCode":"if (newEmail is null)\n    throw new InvalidOperationException(\"newEmail must not be null at this point.\");\nawait _customerRegistrationService.SetEmailAsync(customer, newEmail, requireValidation);","typeGuard":"static bool IsEmailProvided(string email) => email is not null;","tryCatchPattern":null,"preventionTips":["Make the email field non-nullable and [Required] on request models.","Treat a null email as a caller bug, distinct from an invalid-email user error.","Static-analyze call sites for null propagation into SetEmailAsync."],"tags":["customer","email","validation","argument"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}