因此,有关此异常的一些概述。Bizzare ASP.net“对象引用未设置为对象的实例”
我在我的开发机器(windows xp,asp.net 4)上创建了一个网站,并将其定位到.Net 3.5。然后我在舞台机器上部署了100%工作网站(Windows 7,ASP.net 2,IIS 7.5)。
在分析了很多安全问题之后,我终于明白了这个令人困惑的异常,虽然我清楚地理解了它的含义,但我无法理解为什么这个异常会来自堆栈中提到的行跟踪。
...这是你进来的地方! :)
所以堆栈跟踪:
[NullReferenceException: Object reference not set to an instance of an object.]
DVL.Ruby.Admin.Config.ConfigManager..ctor(String path) in C:\Documents and Settings\Andy\Desktop\SS_DVL\Displays\Ruby.root\Ruby\Ruby.Admin\Config\ConfigManager.cs:41
DVL.Ruby.Admin.SettingGroups.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\Andy\Desktop\SS_DVL\Displays\Ruby.root\Ruby\Ruby.Admin\SettingStates.aspx.cs:33
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
ConfigManager.cs
namespace DVL.Ruby.Admin.Config
{
public class ConfigManager
{
private XElement m_appSettings;
private XElement m_appMappings;
private XElement m_appConnectionStrings;
private List<Setting> m_settings;
private List<ConnectionString> m_connectionStrings;
private string m_path;
private ConfigManager() { }
private ConfigManager(string path)
{
if (path==null)
{
throw new NullReferenceException("The web.config path provided cannot be null.");
}
if (File.Exists(path)==false)
{
throw new ArgumentException("The web.config file does not exist.");
}
// Load the file
XDocument configDocument = XDocument.Load(path);
m_path = path; // <-- Line 41
// Do some stuff...
}
/// <summary>
/// Creates and returns a new instance of the ConfigManager
/// </summary>
/// <param name="path">The path of the web.config file to load.</param>
/// <returns>A new instance of the ConfigManager</returns>
public static ConfigManager Load(string path)
{
return new ConfigManager(path);
}
SettingStates.aspx.cs
namespace DVL.Ruby.Admin
{
public partial class SettingGroups : System.Web.UI.Page
{
ConfigManager m_configManager;
List<State> m_states;
protected void Page_Load(object sender, EventArgs e)
{
// Check we have the location of the web.config file we'll be editing
string webConfigPath = WebConfigurationManager.AppSettings[Constants.ApplicationKey_WebConfigPath];
if (webConfigPath == null)
{
return;
}
try
{
// Initialise the manager
m_configManager = ConfigManager.Load(webConfigPath); // <-- Line 33
}
catch (ConfigurationException ex)
{
ErrorMessage.Visible = true;
ErrorMessage.Text = ex.Message;
SaveStateButton.Enabled = false;
LoadStateButton.Enabled = false;
return;
}
所以你可以看到,空引用发生在这条线上:
m_path = path;
这很奇怪,因为我检查路径是否为空(而不是它会有所作为),并且m_path已经为空,但这并不重要,因为我正在分配!
一两件事我注意到看着反射库时是编译版本改动:
this.m_path = path;
我的猜测给出了在其可能失败的逻辑点(即“这个”参考)。但为什么这是空的?这与我的静态Load(string)方法创建对象实例有关吗?如果是这样,为什么它可以在我的开发电脑而不是部署机器上工作?
我很高兴为对象创建一个正常的构造函数,但我只是关注Microsoft如何开发他们的API(ala XDocument)。我试图再次使用反射器,看看他们是否实现了不同的加载方法,但方法是空的(可能是混淆?)。
无论如何,让包裹起来,因此主要问题:
- 为什么没有上线41?
- 为什么在我的开发机器上不会发生?
你不应该抛出'NullReferenceException'。改为抛出'ArgumentNullException'。 – 2010-07-16 11:26:30
好点!虽然没有帮助我的问题! :( – Andy 2010-07-16 11:32:43