【问题标题】:Exception thrown when it shouldn't be [duplicate]不应该抛出的异常[重复]
【发布时间】:2022-01-08 05:23:33
【问题描述】:
    private async Task<long> InsertCustomerRecord(CreateCustomerModel model)
    {

        Int64 phoneNumber = 0;
        var isParsed = Int64.TryParse(model.PhoneNumber, out phoneNumber);
        if (!isParsed)
            phoneNumber = 0;
        var USPSAddressValidatedId = (int)model.USPSAddressValidated;
        try
        {
            IDataParameter[] parameters = new IDataParameter[]
            {
            new SqlParameter("@FirstName", model.FirstName.ToUpper()),
            new SqlParameter("@LastName", model.LastName.ToUpper()),
            //new SqlParameter("@MiddleInitial", model.MiddleInitial),
            new SqlParameter("@AddressLine1",model.AddressLine1.ToUpper() ?? ""),
            new SqlParameter("@AddressLine2",model.AddressLine2.ToUpper() ?? ""),
            new SqlParameter("@City",model.City.ToUpper()),
            new SqlParameter("@State",model.State.ToUpper()),
            new SqlParameter("@CountyCode",model.CountyCode.ToUpper()),
            new SqlParameter("@ZipCode",model.ZipCode),
            new SqlParameter("@DateOfBirth",model.DateOfBirth.ToShortDateString()),
            new SqlParameter("@Phone",phoneNumber),
            new SqlParameter("@Email",model.EmailAddress.ToUpper()??""),
            new SqlParameter("@PersonalRepresentative",model.CustomerClass.ToUpper()),
            new SqlParameter("@ExpiryDate", model.ExpirationDate),
            new SqlParameter("@CustomerClass", model.Customer_Class),
            new SqlParameter("@USPSAddressValidated",USPSAddressValidatedId ),
            new SqlParameter("@ImportFromLegacySystem", model.ImportFromLegacySystem)
            };
        
            return await InsertUpdateProcedure("RF_InsertCustomerCard", parameters);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return 0;
        }
    }

抛出的异常是

{"Object reference not set to an instance of an object."}
    Data: {System.Collections.ListDictionaryInternal}

【问题讨论】:

  • 在哪一行?像这样的代码是可疑的:model.EmailAddress.ToUpper()??"" - 如果 EmailAddress 是一个字符串,它可能为 null,但如果在非 null 实例上调用 ToUpper() 将不会返回 null,所以你不会合并它。如果你知道 EmailAddress可能为 null 然后调用 .ToUpper() 会导致崩溃;代码应该看起来像 model.EmailAddress?.ToUpper()??"" - ?. 不会在 null 电子邮件地址上调用 ToUpper - 它只会直接缩短到 null,然后 ?? 有意义跨度>
  • ListDictionaryInternal 甚至不在代码 sn-p 中,所以我们无法真正判断出什么问题。
  • 您能否提供有关此问题的更多信息/范围?
  • 您可能希望将 null 传播 ?null-coalescing ?? 结合使用,例如model.SomeProperty?.ToUpper() ?? ""UPD:迟到看到我的评论重复了@Caius Jard 的评论。
  • "当它应该是"FTFY时抛出异常

标签: c# exception sqlparameter


【解决方案1】:

如果你有一些你怀疑可能为空的东西:

model.EmailAddress
      ^^^^^^^^^^^^
   this might be null

您需要在其上使用空传播运算符:

model.EmailAddress?.ToUpper() ?? ""
                  ^^

这意味着如果它是null,那么操作链的评估将在.EmailAddress 返回null 时停止,并且子表达式(model.EmailAddress?.ToUpper())将解析为null。然后它将被空合并运算符?? 拾取并变成您放在运算符右侧的任何内容。如果它是一个常量,比如"",那么保证整个表达式不为空

您可以在属性和方法返回值上多次使用这些运算符:

thing.Prop?.Method() ?? thing.OtherProp?.OtherMethod()?.AnotherProp ?? "Constant"

如果集合可能为空,还有一个容空索引器:

//in case the collection is null, or the thing at index 0 is null, or its Property is null...
thing.Collection?[0]?.Property?.Something() ...
                ^^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多