簡體   English   中英

為什么這段代碼會引發NullReferenceException? 是因為引用字符串嗎?

[英]Why does this code throw a NullReferenceException? Is it because of the ref string?

我在這里有點困惑。 我有一個已經運行了多年的程序,就在昨天,我對文件的一部分進行了一些更改。 它開始在引用字符串方法上引發“ NullReferenceException”。

   string message = "";

   this.GetAuditMessage(grpAccess, ref message);


    private void GetAuditMessage(Control c_parent, ref string message)
    {
        foreach(Control c in c_parent.Controls) 
        {       
            Console.WriteLine(c.Name);
            if (c.GetType().ToString().Equals("System.Windows.Forms.Panel"))
            {
                try
                {
                    string str = message;
                    GetAuditMessage(c, ref str);
                }
                catch(Exception nu) 
                {
                    Console.WriteLine(nu.InnerException.ToString());
                }


            }
            else if (c.GetType().ToString().Equals("System.Windows.Forms.TextBox") ||
                c.GetType().ToString().Equals("System.Windows.Forms.ComboBox"))
            {
                int n = c.Tag.ToString().IndexOf(":");
                int len = c.Tag.ToString().Length;

                string controlName = c.Tag.ToString().Substring(0, n);
                string oldSetting = c.Tag.ToString().Substring(n + 1, len - (n + 1));

                if (!oldSetting.Equals(c.Text))
                {
                    message += "Change " + controlName + "' from '" + oldSetting + "' to '" + c.Text + ".'\n";
                }
            }
        }
    }

當我在GetAuditMessage(c, ref message)休息一下時。 它引發異常。 但是,我修改為類似下面的內容。

string str = message;
GetAuditMessage(c, ref str);

以上工作正常。 然后,我嘗試運行舊版本。 它運行正常,無一例外。 請能有人解釋為什么會例外嗎? 請問為什么現在不扔呢? 我如何最好地解決此問題?

我最好的猜測是c.Tagnull ,或者不是完全符合預期的格式-與您的“修復”無關(盡管它確實使它無法正常工作)。 您最好傳入一個StringBuilder ,並使用來構建組成的字符串。 則無需ref

通過對可能為null (即InnerException .ToString()訪問.ToString() ,使catch執行粗心的操作並沒有幫助。 所以; 如果我們假設某個Tag不是字符串或不是預期的格式:它將拋出NullReferenceExceptionArgumentOutOfRangeException而沒有InnerException 您的catch塊,如果被命中(即,如果它不是最外面的控件-窗體本身),則將依次拋出NullReferenceException

如果是我:

var sb = new StringBuilder();
this.GetAuditMessage(grpAccess, sb);
var message = sb.ToString();


private void GetAuditMessage(Control c_parent, StringBuilder sb) {
    foreach(Control c in c_parent.Controls)  {       
        Console.WriteLine(c.Name);
        if (c is System.Windows.Forms.Panel) {
            try {
                GetAuditMessage(c, sb);
            } catch(Exception nu)  {
                Console.WriteLine(nu.Message);
            }
        }
        else if (c is System.Windows.Forms.TextBox ||
            c is System.Windows.Forms.ComboBox)
        {
            string tag = c.Tag == null ? "" : c.Tag.ToString();
            int n = tag.IndexOf(":");
            if(n < 0) continue;
            int len = tag.Length;

            string controlName = tag.Substring(0, n);
            string oldSetting = tag.Substring(n + 1, len - (n + 1));

            if (!oldSetting.Equals(c.Text))
            {
                sb.AppendFormat("Change {0} from '{1}' to '{2}'.",
                    controlName, oldSetting, c.Text).AppendLine();
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM