How to create instances of uvm component whose size is defined dynamically?

I am converting system verilog class based testbench to UVM. I had to replace it’s constructor with UVM constructor and had to replace all new with create. I was deciding the array length dynamically when i create them by passing parameter with new. Ex : class_name object=new() and it’s is not possible now since UVM constructor does not support the my format. So I wrote a new function which I call after I instantiate the object in UVM using create method which does job for me but dynamic array length works well with new method but I could not find workaround to make it work with UVM.

Earlier code:

class child extends parent_class;
protected parent_class array [];

function new (int number_inst = 1);
   begin
      super.new(1);
      // Create the arrays of pointers to the array      
      array = new[number_inst];
      
   end
endfunction               

endclass

Current code:

class child extends parent_class;


`uvm_component_utils(child)
function new(string name = "child", uvm_component parent = null);
  super.new(name, parent);
  endfunction

protected parent_class array [];

function initialize_setup (int number_inst = 1);
   begin
      super.initialize_setup(1);
          
     for(int i=0;i< number_inst;i++) 
     begin
         array[i] = parent_class::type_id::create($sformatf("array%0d",i), this);
         array[i].initialize_setup(1);
     end   
   end
endfunction
endclass                     

The current gives me following error:

Error-[NOA] Null object access at array[i].initialize_setup(1);
The object is being used before it was constructed/allocated.
Please make sure that the object is allocated before using it.

Problem gets solved when I hardcode the value array instance Ex: protected parent_class array [20];

What could be solution for this type of problem ?

Thanks,

Varun

In reply to Varunshivashankar:
Write your function to
new
the dynamic array first.

function initialize_setup (int number_inst = 1);
  super.initialize_setup(1);
  array = new[number_inst]; // you forgot to do this 
  foreach(array[ii]) // now you can use a foreach instead of for loop
    begin
      array[ii] = parent_class::type_id::create($sformatf("array%0d",ii), this);
      array[ii].initialize_setup(1);
    end   
endfunction

In reply to dave_59:

Using new and then overriding works well but isn’t create method suppose to take the place of new ?

In reply to Varunshivashankar:
You use the create() method to construct classes that have been registered with factory. The dynamic array new is not a class constructor; it just shares the same SystemVerilog keyword.