【问题标题】:why the if condition doesn't check the nulls为什么 if 条件不检查空值
【发布时间】:2012-05-10 03:20:00
【问题描述】:

我试图检查我的 var 中的空值,但它抛出“对象引用未设置为对象的实例”。

 private void GenerateImage()
    {
        //Webster.Client.Modules.Metadata.Helper test = new Webster.Client.Modules.Metadata.Helper();
         var selectedstory = Webster.Client.Modules.Metadata.Helper.SelectedStoryItem;

        if((selectedstory.Slug).Trim()!=null)
        {
         //if (!string.IsNullOrEmpty(selectedstory.Slug))
       //{

           if (File.Exists(pathToImage))
           {
              }
           else
           {
               this.dialog.ShowError("Image file does not exist at the specified location", null);
           }
       }
       else
       {
           this.dialog.ShowError("Slug is Empty,please enter the Slug name", null);
       }
    }

我知道 selectedstory.Slug 的值为 null,这就是为什么我使用 if 条件进行检查,但它在 if 条件下直接抛出。

谁能告诉我什么是正确的检查方法。

【问题讨论】:

  • C# 应该有一个safe-navigation operator 有一个关于 C# 的建议,但它是 not yet implemented 一些有进取心的用户 made one 为自己而他们迫不及待
  • 感谢 Michael 提供的有用链接。我不太了解扩展方法的使用,但现在很好理解:)。

标签: c# .net string


【解决方案1】:

您不能在空引用上调用方法。取出.Trim()

【讨论】:

  • 你会建议 OOP 为 selectedStory.Slug 添加一个空检查,还是将他的代码包含在 try/catch 中?
  • OP 已经有一个空检查,只是一个不需要的 .trim() 也导致了问题。如果删除了 Trim() 调用,代码将按预期工作。
  • 如果selectedstory 为空,那么对selectedstory.Slug 的空检查将引发空引用异常,因为您正在尝试引用空引用的属性。
  • @PaulSasik,是的,但 selectedstory 不是 null,selectedstory.Slug 是。 OP在问题中已经说过了。问题不是.Slug,而是.Trim()
【解决方案2】:
if((selectedstory.Slug).Trim()!=null)

将首先在字符串上调用Trim() 方法,然后检查是否为空。这是失败的部分:您正在尝试在空对象上调用实例方法。

你想要的是这样的:

if ( selectedstory != null && string.IsNullOrEmpty(selectedstory.Slug) )

【讨论】:

  • 感谢大家,为我提供了这么多选择的启发,@Muad'Dib 代码对我有用。经过测试,最终看起来不错。再次感谢
  • ,对不起,我不能使用你的这部分代码,它总是说 null bcoz 我的一些 var 在那个对象“selectedstory != null”中是 null
【解决方案3】:

试试这个:

if (!string.IsNullOrWhiteSpace(selectedstory.Slug))

这消除了对您正在检查的属性调用 Trim 的需要。

【讨论】:

  • +1 用于使用 IsNullOrWhiteSpace,从而无需调用 Trim 方法
  • 需要注意的是 IsNullOrWhiteSpace 仅在 .net 4 及以上版本中可用
  • @Leo 感谢您阐明这一点——我在答案中添加了该注释。
【解决方案4】:

这就是我最终想到的

    try
        {

            if (!string.IsNullOrWhiteSpace(selectedstory.Slug))
            {

                if (File.Exists(pathToImage))
                {
                    string SlugName = selectedstory.Slug;
                    if (pathToImage.Contains(SlugName))
                    {

                    }
                    else
                    {
                        this.dialog.ShowError("Image file name is not same as Slug name", null);
                    }
                }
                else
                {
                    this.dialog.ShowError("Image file does not exist at the specified location", null);
                }

            }
          }
        catch (Exception ex)
        {
            this.dialog.ShowError("Slug is Empty,please enter the Slug name", null);

        }
    }

【讨论】:

  • @Rohit 不确定如何给予信任,因为我使用了 Joe White、Kevin 和 dbaseman 的想法和代码的专家评论
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 2017-07-08
  • 2014-07-17
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 2018-06-13
相关资源
最近更新 更多