Logging class using delegates (NullReferenceException)

Posted by Leroy Jenkins on Stack Overflow See other posts from Stack Overflow or by Leroy Jenkins
Published on 2010-04-14T17:42:35Z Indexed on 2010/04/14 17:53 UTC
Read the original article Hit count: 320

I have created a small application but I would now like to incorporate some type of logging that can be viewed via listbox. The source of the data can be sent from any number of places. I have created a new logging class that will pass in a delegate. I think Im close to a solution but Im receiving a NullReferenceException and I don’t know the proper solution. Here is an example of what Im trying to do:

Class1 where the inbound streaming data is received.
class myClass
{
   OtherClass otherClass = new OtherClass();
   otherClass.SendSomeText(myString);
}

Logging Class

class OtherClass
{
   public delegate void TextToBox(string s);

   TextToBox textToBox;

    Public OtherClass()
    {
    }

   public OtherClass(TextToBox ttb) 
   {
       textToBox = ttb;
   }

   public void SendSomeText(string foo)
   {
       textToBox(foo);
   }
}

The Form

public partial class MainForm : Form
   {
   OtherClass otherClass;

   public MainForm()
   {
       InitializeComponent();
       otherClass = new OtherClass(this.TextToBox);
   }

   public void TextToBox(string pString)
   {
       listBox1.Items.Add(pString);
   }

}

Whenever I receive data in myClass, its throwing an error. Any help you could give would be appreciated.

© Stack Overflow or respective owner

Related posts about c#

Related posts about nullreferenceexception