I am kind of confused here . I have a program that has been running for years , Just yesterday when i made some few changes in one part of the file. It begin throwing "NullReferenceException" on ref string method.
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";
}
}
}
}
When i put a break at GetAuditMessage(c, ref message)
. It throws exception . However, i modify to something like below.
string str = message;
GetAuditMessage(c, ref str);
The above works fine. I then try and run the old version . It run fine with no exception. Please can some one explain why the exception ? Please why does it throw now and doesn't before ? How do i best fix this ?