因此,有關此異常的一些概述。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