2010-07-16 65 views
1

因此,有關此異常的一些概述。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?
  • 爲什麼在我的開發機器上不會發生?
+1

你不應該拋出'NullReferenceException'。改爲拋出'ArgumentNullException'。 – 2010-07-16 11:26:30

+0

好點!雖然沒有幫助我的問題! :( – Andy 2010-07-16 11:32:43

回答

0

好吧,

因此,基本上,可能是一個小白錯誤。該網站是在發佈,我猜這意味着它不包含所有的調試信息,因爲它是行號是錯誤的(第41行應該是指針進一步進入構造函數,其中一個更容易理解的問題做與配置發生)。

一切都很好,但它讓我有點bu。。 ASP.net編譯版本與控制檯或其他應用程序不同嗎?我確定我有一個堆棧跟蹤,其中包含一個來自Release的正確行。

此外,爲什麼行號在堆棧跟蹤下更正確?即線33是正確的。這純粹是巧合嗎?

Ta!

Andy。

+0

發佈版本沒有所有的調試信息,所有註釋都被刪除等等。所以你不能得到linie數字代碼錯誤,但是如果你包含* .pdb文件,你仍然可以得到調試信息,因爲它存儲在.pdb文件中 – Holger 2013-10-23 14:12:34

相關問題