Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8153769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:07:45+00:00 2026-06-06T16:07:45+00:00

First of all i’m new in here and new with csharp. just exhausted while

  • 0

First of all i’m new in here and new with csharp. just exhausted while doing some exercises for learning csharp. cant find a solution why i get the error “NullReferenceException was unhandled” and

1) How to overcome this? according to my researches its related with initialisation but couldnt make it. i debug and watched the values all buttons gets gets null values is it because of this? how can i solve it? what to do? which code to add?

(***all informing knowledge about class initialisations and arrays and null stuffs are welcome. i wanna learn all the points and wanna be expert 😛 and people can optimize the code as well. all extra informations are welcome.)

2) And why compiler doesnt show error but error comes while running the code?

ok now i have the code blow

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace temp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Button[] btn = new Button[5];

            for (int i = 0; i < 5; i++)
            {
                btn[i] = new Button();
                btn[i].Width = 50;
                btn[i].Height = 50;
                flowLayoutPanel1.Controls.Add(btn[i]);
                btn[i].Click += new EventHandler(btn_Click);
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            Button[] btn = sender as Button[];
            btn[3].Text = "button name changed";  // here problems occurs
            //btn[3].BackColor = Color.Red;   // here problems occurs
            // btn[3].PerformClick();    // here problems occurs
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:07:47+00:00Added an answer on June 6, 2026 at 4:07 pm

    it should be:

    void btn_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        btn.Text = "button name changed";  // here problems occurs
    }
    

    the sender is the button itself, not the array

    UPDATE ACCORDING TO THE COMMENT:

    you should do something like this:

    public partial class Form1 : Form
    {
        private Button[] m_ButtonsArray;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            m_ButtonsArray = new Button[5];
    
            for (int i = 0; i < 5; i++)
            {
                m_ButtonsArray [i] = new Button();
                m_ButtonsArray [i].Width = 50;
                m_ButtonsArray [i].Height = 50;
                flowLayoutPanel1.Controls.Add(m_ButtonsArray [i]);
                m_ButtonsArray [i].Click += new EventHandler(btn_Click);
            }
        }
    
        void btn_Click(object sender, EventArgs e)
        {
            m_ButtonsArray[3].Text = "button name changed";  // here problems occurs
        }
    }
    

    UPDATE – Changing the 2nd button when the 5th button was clicked:

    public partial class Form1 : Form
    {
        private Button[] m_ButtonsArray;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            m_ButtonsArray = new Button[5];
    
            for (int i = 0; i < 5; i++)
            {
                m_ButtonsArray [i] = new Button();
                m_ButtonsArray [i].Width = 50;
                m_ButtonsArray [i].Height = 50
                m_ButtonsArray [i].Tag = i;;
                flowLayoutPanel1.Controls.Add(m_ButtonsArray [i]);
                m_ButtonsArray [i].Click += new EventHandler(btn_Click);
            }
        }
    
        void btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (((int)btn.Tag) == 5)
            {
               m_ButtonsArray[2].Text = "your text here";
            }
        }
    }
    

    explanation:
    when i create the button, i add the index as a tag (the button knows it’s index with the Tag property)
    when a button is clicked, i check the sender object – casting it to Button, for the index that kept in the Tag. if the Tag value is 5 then i reference the second button (i can reference it though m_ButtonsArray) and change the values of it

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first of all some details: I configured security as below in web.xml view plaincopy
First of all, I'm quite new to the Android and JAVA world (coming from
First of all, apologize because I have seen some posts about this, but I
First of all I've gone through dozens of posting here on SO and google
First of all, some background: I'm trying to get in a list of integers
First all, I am still learning CakePHP and I am close to ZERO using
First of all, I just installed XE2 for the first time, and plan to
First of all there is a question with the same title here on SO
First of all Im new to vb 2010 and so far have enjoyed what
First of all, here's my config: VS2010/Debug/C++ Win32 Project/Vista Home Premium Invoking GetOpenFileName via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.