C# System.NullReferenceException 错误 Google 日历
C# System.NullReferenceException error Google Calendar
我有一个程序,我在其中不断收到 NullReferenceException 错误。这是导致错误的代码:
string description = "";
if (string.IsNullOrEmpty(eventItem.Description.ToString()))
{
description = "No description available.";
}
else
{
description = eventItem.Description.ToString();
}
我已经仔细阅读了这个 post (What is a NullReferenceException and how do I fix it?),并且我已经尝试了几种解决方案(恐怕我只是对它们的理解还不足以尝试它们) ,但我就是不明白为什么会这样。据我了解,发生此错误是因为该字符串实际上为空。
我的 Google 日历上有些事件没有输入描述,因此描述为空,但我的代码不应该检查并处理它吗?或者,是调用IsNullOrEmpty方法时eventItem.Description.ToString()不能为null的问题?我也尝试将 if 语句更改为:
if (eventItem.Description.ToString() == null)
...但我仍然收到 NRE 错误。我尝试过以多种不同的方式重写我的代码,但没有任何效果。我已经筋疲力尽了!
您的 eventItem
本身可能为空。做一些事情
if ( eventItem!= null && eventItem.Description != null && eventItem.Description.ToString() == null)
在 DStanley 在评论中指出 .ToString() == null 比较是不必要的之后更新。
if ( eventItem!= null && eventItem.Description != null)
{
}
只是为了帮助您更好地理解... NullReferenceException 基本上意味着您正在尝试使用某个对象而不实例化它。最简单的预防方法之一是在您不确定对象是否不为空时添加空检查。当我说添加 null 检查时,它只是意味着在访问对象的任何 属性 之前在 if 块中与 null 进行比较。
if( objectName != null)
{
//then do something on the object
}
shouldn't the code I have check for that, and handle it?
如果 item
或 item.Description
为 null,则不会。如果 item
为 null,则调用 .Description
将抛出空引用异常,如果 item.Description
为 null,则调用 ToString
将抛出空引用异常。没有 "magic" 可让您在空引用上调用 ToString
。
请注意,如果 item.Description
已经是一个字符串,则无需调用 ToString()
。只是做:
if (string.IsNullOrEmpty(eventItem.Description))
{
description = "No description available.";
}
else
{
description = eventItem.Description;
}
我有一个程序,我在其中不断收到 NullReferenceException 错误。这是导致错误的代码:
string description = "";
if (string.IsNullOrEmpty(eventItem.Description.ToString()))
{
description = "No description available.";
}
else
{
description = eventItem.Description.ToString();
}
我已经仔细阅读了这个 post (What is a NullReferenceException and how do I fix it?),并且我已经尝试了几种解决方案(恐怕我只是对它们的理解还不足以尝试它们) ,但我就是不明白为什么会这样。据我了解,发生此错误是因为该字符串实际上为空。
我的 Google 日历上有些事件没有输入描述,因此描述为空,但我的代码不应该检查并处理它吗?或者,是调用IsNullOrEmpty方法时eventItem.Description.ToString()不能为null的问题?我也尝试将 if 语句更改为:
if (eventItem.Description.ToString() == null)
...但我仍然收到 NRE 错误。我尝试过以多种不同的方式重写我的代码,但没有任何效果。我已经筋疲力尽了!
您的 eventItem
本身可能为空。做一些事情
if ( eventItem!= null && eventItem.Description != null && eventItem.Description.ToString() == null)
在 DStanley 在评论中指出 .ToString() == null 比较是不必要的之后更新。
if ( eventItem!= null && eventItem.Description != null)
{
}
只是为了帮助您更好地理解... NullReferenceException 基本上意味着您正在尝试使用某个对象而不实例化它。最简单的预防方法之一是在您不确定对象是否不为空时添加空检查。当我说添加 null 检查时,它只是意味着在访问对象的任何 属性 之前在 if 块中与 null 进行比较。
if( objectName != null)
{
//then do something on the object
}
shouldn't the code I have check for that, and handle it?
如果 item
或 item.Description
为 null,则不会。如果 item
为 null,则调用 .Description
将抛出空引用异常,如果 item.Description
为 null,则调用 ToString
将抛出空引用异常。没有 "magic" 可让您在空引用上调用 ToString
。
请注意,如果 item.Description
已经是一个字符串,则无需调用 ToString()
。只是做:
if (string.IsNullOrEmpty(eventItem.Description))
{
description = "No description available.";
}
else
{
description = eventItem.Description;
}