对象引用未设置为对象的实例。(会话) [英] Object reference not set to an instance of an object.(Session)
本文介绍了对象引用未设置为对象的实例。(会话)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
i我为会话变量创建一个属性
hello,
i am create a Property for session variable
public static string CurrentFolderPath
{
get { return System.Web.HttpContext.Current.Session["FolderPath"].ToString(); }
set { System.Web.HttpContext.Current.Session["FolderPath"] = value; }
}
但当我检查值时,它通过错误对象引用未设置为对象的实例
but when i am checking the value then it through error Object reference not set to an instance of an object
if (SessionManager.CurrentFolderPath != string.Empty)
{
path = CommonFunctions.UserFolder;
}
通过错误。请帮帮我如何检查此变量值。
it Through error. Please Help me How to Check This variable value.
推荐答案
您可能需要在物业的getter中添加空检查
you may need to add null check in getter of the property
public static string CurrentFolderPath
{
get
{
if (HttpContext.Current.Session["FolderPath"] == null)
return string.Empty;
else
return (string)HttpContext.Current.Session["FolderPath"];
}
set
{
HttpContext.Current.Session["FolderPath"] = value;
}
}
旁注:要检查字符串为空,可以使用字符串.IsNullOrEmpty
方法
side note: to check string empty you can use string.IsNullOrEmpty
method
if (!string.IsNullOrEmpty(SessionManager.CurrentFolderPath))
{
path = CommonFunctions.UserFolder;
}
这篇关于对象引用未设置为对象的实例。(会话)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文