Constrain the number of occurrences in an array

In reply to dave_59:
I tried with this:

class c;
  rand enum {ADD, SUB, MUL, DIV} e;
endclass

class txn;
rand c c_inst[];

  function void pre_randomize();
    for(int i=0;i<10;i++)begin
      c_inst[i]= new();
      c_inst[i].randomize();
    end
  endfunction

constraint add_less_than_5 {c_inst.sum() with (int'(item.e == c::ADD)) == 5;}

endclass

module abc;
  txn t1;
  
  initial begin
    t1=new();
    repeat(10) begin
      assert(t1.randomize());
      $display("%p",t1);
    end
  end
endmodule

but getting this

# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
#    Time: 0 ns  Iteration: 0  Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
#    Time: 0 ns  Iteration: 0  Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
#    Time: 0 ns  Iteration: 0  Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
#    Time: 0 ns  Iteration: 0  Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
#    Time: 0 ns  Iteration: 0  Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
#    Time: 0 ns  Iteration: 0  Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.

Can you please tell me reason for this?