2
Reply

Null Exception - No Active Form

Noah NU

Noah NU

Aug 6 2010 2:54 PM
2.7k

Hello, I have two classes, 'frmMain', which is the main class behind Form1, and 'FTPClient' which is called from frmMain.
On the frmMain form I have a richtextbox called rtbOutput. I am trying to access the richtextbox from FTPClient.
I wrote this to send text to the RichTextBox on frmMain:
 public void Output(string text, string 
category)
        {
            RichTextBox rtbOutput = (RichTextBox)frmMain.ActiveForm.Controls["rtbOutput"];
            rtbOutput.SelectionStart = rtbOutput.Text.Length;
            rtbOutput.SelectionColor = Color.Black;
            rtbOutput.SelectionFont = new Font(rtbOutput.Font, FontStyle.Regular);
            rtbOutput.SelectedText = "Status: " + category + " : " + text + Environment.NewLine;
            rtbOutput.ScrollToCaret();
        }

I am getting an error on the second line where SelectionStart and Text.Length are being accessed. The error is that an object has not been set. I checked the value of rtbOutput and it is null.. I checked the value of frmMain.ActiveForm and it is null too..
So now there are two points to my question. Why is frmMain.ActiveForm null and is there another way to go about passing text from FTPClient to the RichTextBox belonging to the frmMain class?

Answers (2)