NullReferenceException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bobby1043
    New Member
    • Jul 2008
    • 2

    NullReferenceException

    So I have a problem in C#. I am new to C# so you have to bear with me.

    I have alot of buttons I created at startuptaht I want to change text with (button1,button 2, ...) and I want to automate the process, instead of just writing each line out.

    The problem is that every time I run the program, I get a NullReferenceEx ception "Object reference not set to an instance of an object."
    It is in the line "this.Contr ols[string.Format(" button{0}", n)].Text = "sometext"; "

    I am out of ideas, any help would be great.

    Here is part of my code

    private void button1_Click(o bject sender, EventArgs e)
    {
    Button button2 = new Button();
    Button button3 = new Button();

    int n = 1;
    while (n <= 2)
    {
    int n1 = (n * 27) + 27;
    this.Controls[string.Format(" button{0}", n)].Text = "sometext";
    this.Controls[string.Format(" button{0}", n)].Size = new Size(57, 21);
    this.Controls[string.Format(" button{0}", n)].Location = new Point(11, n1);
    this.Controls.A dd(this.Control s[string.Format(" button{0}", n)]);
    n++;
    }
  • vanc
    Recognized Expert New Member
    • Mar 2007
    • 211

    #2
    Have you tried to specified those buttons in the loop to see if it works or not?

    Comment

    • deathprincess
      New Member
      • Jul 2008
      • 12

      #3
      Originally posted by vanc
      Have you tried to specified those buttons in the loop to see if it works or not?
      Have you tried creating just one button?
      If not yet, just try your code with just a button..

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Nullreference means you are dereferencing a null value. Make sure your flow is correct. i.e Don't try to retrive the buttons and set their text before adding them!

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          There appears to be a number of things wrong with this.
          I'll help out a little.
          [code=c#]
          Button button2 = new Button();
          [/code]
          This create a new button yes, but does not add it to this.Controls unless you tell it to with a
          [code=c#]
          this.Controls.A dd(button2);
          [/code]

          Continuing, just because you called the object button2, does not mean the .Name property contains that value. You need to see it manually
          [code=c#]
          Button button2 = new Button();
          button2.Name="b utton2";
          //note there is no sizing, location or visual-display information provided to the button
          this.Controls.A dd(button2);
          [/code]

          As for your NullReference exception:
          [code=c#]
          this.Controls[string.Format(" button{0}", n)].Text = "sometext";
          [/code]
          If this.Controls cannot find a control with the name you specified it will return a Null. If it does, you are then doing Null.Text, which will fail because a Null is not an instance of any object.

          Comment

          • bobby1043
            New Member
            • Jul 2008
            • 2

            #6
            Thank you very much for the help. It works now.

            Comment

            Working...