Hello, the application on winforms, which will be "simulated" banking queue. An array of queues Queue<int>[] QArray , the number of queues is sent from the textbox, then numbers are written to each of the queues, the number of clients in the queue is output to the labels.

 M = int.Parse(textBox1.Text); //Количество очередей QArray = new Queue<int>[M]; int element = 0; for(int i=0;i< M; i++) { Random rnd = new Random(); int m = rnd.Next(1, 8);//Количество элементов в очереди for (int j = 0; j <m; j++) { element = rnd.Next(3, 30); QArray[i].Enqueue(element); // ОШИБКА } } for (int i=0;i< M; i++) { var labelnumber = i; var label = new Label(); { Location = new Point(46, 132 + i * 15); Text = QArray[M].Count.ToString() ; } this.Controls.Add(label); } 

After entering the value in the textbox, the exception appears “Object reference does not indicate an object instance.” Tell me what the problem is, please.

  • 2
    When you correct the mistake that andreycha explained to you, others are waiting for you. var label = new Label(); - transfer ";" in after "}". QArray[M] - index out of bounds. - Igor
  • 2
    And please, do not use single-letter variable names that differ only in case. - Igor

1 answer 1

You have initialized an array of queues:

 QArray = new Queue<int>[M]; 

But did not create the queues themselves . Do this:

 QArray = new Queue<int>[M]; int element = 0; for (int i = 0; i < M; i++) { QArray[i] = new Queue<int>(); // остальной код }