views:

429

answers:

5

I have 2 Forms (Form1 and Form2) and a class (Class1). Form1 contains a button (Button1) and Form2 contains a RichTextBox (textBox1) When I press Button1 on Form1, I want the method (DoSomethingWithText) to be called. I keep getting "NullReferenceException - Object reference not set to an instance of an object". Here is a code example:

Form1:

namespace Test1
{  

    public partial class Form1 : Form  
    {

        Form2 frm2;

        Class1 cl;

        public Form1()  
        { 
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frm2 = new Form2(); 
            cl.DoSomethingWithText();
            frm2.Show()
        } 
   }  
}

Class1:

namespace Test1
{

      class Class1
      {

           Test1.Form2 f2;
           public void DoSomethingWithText()
           {
                f2.richTextBox1.Text = "Blah blah blah";
           }
      }
}

How can I call this method from within a class? Any help is greatly appreciated.

+10  A: 

Hi Nate,

You have to instantiate c1 and f2. Try this:

public partial class Form1 : Form  
{
    Form2 frm2;
    Class1 cl;

    public Form1()  
    {  
        c1 = new Class1();
        InitializeComponent();  
    }

    private void button1_Click(object sender, EventArgs e)
    {
      frm2 = new Form2();
      cl.DoSomethingWithText(frm2);
      frm2.Show();
    } 
  }

  class Class1
  {

       public void DoSomethingWithText(Test1.Form2 form)
       {
            form.richTextBox1.Text = "Blah blah blah";
       }
  }

UPDATE

As Keith has pointed out, because you're instanciating a new version of Form2, the rich textbox will never show the blah blah blah code. I've updated the sample to fix this.

Ray Booysen
This will compile, but won't quite work as expected - Class1 instantiates its own copy of Form2, so the one being displayed won't have the results of DoSomethingWithText. To fix either pass frm2 to c1 somehow or call cl.f2.Show() instead.
Keith
Thanks Keith I've updated the code.
Ray Booysen
The original question was how to get rid of the NullReferenceException, however it's always nice to see the problem solved rather than the question answered!
Hooloovoo
Since Class1 does not save any state, the DoSomethingWithText method can be made static, and then you don't need to instantiate it (or keep a reference to it), but can rather just call `Class1.DoSomethingWithText(frm2)`.
Fredrik Mörk
A: 

You need to either declare DoSomethingWithText as a static class or instantiate the reference to Class1.

public static void DoSomethingWithText()           
  {                
    //Code goes here;           
  }
Hooloovoo
+1  A: 

You are never initializing cl (or f2 for that matter).

Ed Swangren
+2  A: 

You haven't instantiated an instance of Class1 before you've tried to use it

You'd need to do:

private void button1_Click(object sender, EventArgs e)
{
    c1 = new Class1();
    frm2 = new Form2();
    cl.DoSomethingWithText(frm2);
    frm2.Show();
}

Not I've also added in the passing of frm2 in to the DoSomethingWithText method for it to then use (else you'd end up with another similar exception as f2 hasn't been instantiated in that class.

AdaTheDev
+1  A: 

Either instantiate first (see @Ray Booysen's answer) or convert it to a static method:

class Class1
{
   public static void DoSomethingWithText( Test1.Form2 f2 )
   {
      f2.richTextBox1.Text = "Blah blah blah";
   }
}

Then:

 frm2 = new Form2();
 Class1.DoSomethingWithText( frm2 );
 frm2.Show();
Keith
+1: saw your answer after commenting on Ray's.
Fredrik Mörk