【发布时间】: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 提供的有用链接。我不太了解扩展方法的使用,但现在很好理解:)。