Error in array of Labels - C++ Winforms

José Carlos 886 Reputation points
2023-01-23T22:47:10.9133333+00:00

Friends.

I'm developing a hangman game in C++ Winforms, practically ready, but it's giving an error that I couldn't solve. I want to create an array of Labels. This array has no fixed size. At the beginning of the program I put: static array<Label^>^ labels; To create the labels that will contain the letters of the word drawn, I developed the following snippet:

It is giving the error Object reference not set to an instance of an object in line labels[i] = gcnew Label();

Tanks


		void IniciarLabels()
		{
			tam = palavra->Length;

			for (int i = 0; i < tam; i++)
			{
				labels[i] = gcnew Label();
				labels[i]->Font = gcnew System::Drawing::Font("Arial", 26, FontStyle::Bold);
				labels[i]->ForeColor = Color::Black;
				labels[i]->TextAlign = ContentAlignment::MiddleLeft;
				labels[i]->AutoSize = false;
				labels[i]->Size = System::Drawing::Size(40, 40);
				labels[i]->Text = palavra[i] == ' ' ? " " : "_";
				labels[i]->Location = i == 0 ? System::Drawing::Point(X, Y + 5) : System::Drawing::Point((labels[i - 1]->Location.X + 43), Y + 5);
				panel1->Controls->Add(labels[i]);
			}
		}
Developer technologies C++
Developer technologies Visual Studio Other
0 comments No comments
{count} votes

Accepted answer
  1. Alan Farias 755 Reputation points
    2023-02-24T15:38:21.4266667+00:00

    The error "Object reference not set to an instance of an object" usually means that you are trying to access an object that has not been initialized yet. In your case, the labels array is declared as a static variable, but it has not been initialized to a specific size.

    To fix the error, you need to initialize the labels array with a specific size before using it in the IniciarLabels() function. One way to do this is to create a new array with the desired size and assign it to the labels variable, like this:

    static array<Label^>^ labels = gcnew array<Label^>(MAX_LABELS);
    

    Where MAX_LABELS is a constant that defines the maximum number of labels that you expect to use in your program.

    Alternatively, if you don't know the size of the array in advance, you can use a List instead of an array, like this:

    static List<Label^>^ labels = gcnew List<Label^>();
    

    Then, in your IniciarLabels() function, you can add labels to the list using the Add method:

    void IniciarLabels()
    {
        tam = palavra->Length;
    
        for (int i = 0; i < tam; i++)
        {
            Label^ label = gcnew Label();
            label->Font = gcnew System::Drawing::Font("Arial", 26, FontStyle::Bold);
            label->ForeColor = Color::Black;
            label->TextAlign = ContentAlignment::MiddleLeft;
            label->AutoSize = false;
            label->Size = System::Drawing::Size(40, 40);
            label->Text = palavra[i] == ' ' ? " " : "_";
            label->Location = i == 0 ? System::Drawing::Point(X, Y + 5) : System::Drawing::Point((labels[i - 1]->Location.X + 43), Y + 5);
            panel1->Controls->Add(label);
            labels->Add(label);
        }
    }
    

    This way, you can add labels to the list dynamically without having to worry about the size of the array.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.