Error in array of Labels - C++ Winforms

José Carlos 886 Reputation points
2023-01-23T22:52:54.6333333+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(); Thanks


		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
{count} votes

Accepted answer
  1. WayneAKing 4,931 Reputation points
    2023-01-24T05:00:20.2633333+00:00

    You appear to have cretaed an array with no elements and no size. You then attempt to assign a new element to a slot in that array via [i] which location doesn't exist.

    If your array had a size then you won't get that error, as long as indexer i dosen't exceed that size minus 1.

    // Cretae an array of 100 nullptrs.
    array<Label^>^ labels = gcnew array<Label^>(100);
    
    int i = 0;
    
    labels[i] = gcnew Label(); // OK now
    
    
    • Wayne
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2023-01-24T05:30:49.4533333+00:00

    Note that you can start with an empty array and expand its size as you add elements. There is a fair amount of additional overhead doing this, as new memory allocations have to be created each iteration and the existing elements copied to the new array.

    array<Label^>^ labels2;
    for (int n = 0; n < 10; ++n)
    {
        Array::Resize(labels2, n + 1);
        labels2[n] = gcnew Label();
    }
    
    
    • Wayne

  2. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2023-01-24T05:33:47.8733333+00:00

    Hi, José Carlos Souza

    System.Array is reference type.
    Add gcnew array <Label^>(size)before using it.
    You could refer to How to: Use Arrays in C++/CLI

    array< MyClass^ >^ MyClass0;
    MyClass0 = Test0();
    
    
    array<MyClass^>^ Test0() {
       int i;
       array< MyClass^ >^ local = gcnew array< MyClass^ >(ARRAY_SIZE);
    
       for (i = 0 ; i < ARRAY_SIZE ; i++) {
          local[i] = gcnew MyClass;
          local[i] -> m_i = i;
       }
       return local;
    }
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.