堆栈跟综:System.NullReferenceException: 未将对象引用设置到对象的实例。我在Global.asax文件的Application_Start中添加了一个Timer事件,在事件中读取XML文档总是出错,未将对象引用设置到对象的实例。但是我把代码复制到WEB页面上运行却是可以的,让Timer事件定时执行就是读取不到总是出错,请高手指点哪里有问题?public static string WebSiteXml(string XmlPath, string XmlName)
{
string path = HttpContext.Current.Server.MapPath(XmlPath);
XmlDocument xml = new XmlDocument();
xml.Load(path);
XmlNode node = xml.SelectSingleNode(XmlName);
return node.InnerText;
}
调用:
bool AAA = Convert.ToBoolean(BbsyxConfig.WebSiteXml("/Inc/ABC.xml", "/xmlhtml/AAA"));
{
string path = HttpContext.Current.Server.MapPath(XmlPath);
XmlDocument xml = new XmlDocument();
xml.Load(path);
XmlNode node = xml.SelectSingleNode(XmlName);
return node.InnerText;
}
调用:
bool AAA = Convert.ToBoolean(BbsyxConfig.WebSiteXml("/Inc/ABC.xml", "/xmlhtml/AAA"));
事件里面,有些对象是不能使用的。
参见
http://dotnet.aspx.cc/file/Getting-Website-absolute-path-in-asp.net.aspx
这种错误 80%以上都是 有对象是null 或没有初始化对象 就调用了对象里的方法
HttpContext.Current.Server这个不能用,要不就是你路径不对
这个不能用,那用什么替代?我也感觉是这个有问题
HostingEnvironment.MapPath()
不是
HttpContext.Current.Server.MapPath()