Search code examples
oracle-databaseobjecttypescollectionsinitialization

ora-6530 reference to uninitialized composite


I created object and procedure

create or replace type rec_int_g as object
( i integer);
create or replace type typ_int_g is table of rec_int_g;

Procedure iparse(v_str in varchar2, t_int out NOCOPY typ_int_g) is
  /* parsing procedure for blank divided string */
  str         varchar2(4000);
  int_array typ_int_g;

  i   pls_integer := 1;
  pos pls_integer := 1;

begin
  int_array := typ_int_g();
  str := norm(v_str); /* normalize string excluding symbols */

  while pos > 0 loop
    
    pos := instr(str, ' ');
    int_array.extend();
    
  
    if pos > 0 then
      int_array(i).i := to_number(trim(substr(str, 1, pos)));

Then I call it

declare
arr typ_int_g;

begin

search.iparse('1 2 3 4 5',arr);


end;

And I got ora-6530 reference to uninitialized composite here

int_array(i).i := to_number(trim(substr(str, 1, pos)));

I have initialized it yet but got error.


Solution

  • You are using the record type syntax instead of object type syntax to assign value to the element of NT.

    You need to use following syntax:

    int_array(i) := rec_int_g(to_number(trim(substr(str, 1, pos))));
    

    Also, You need to increment the value of index parameter i and do substring of str in each iteration to remove the already calculated spaces.

    I have updated your code as follows:

    create Procedure iparse(v_str in varchar2) is
      /* parsing procedure for blank divided string */
      str         varchar2(4000);
      int_array typ_int_g;
      i   pls_integer := 1;
      pos pls_integer := 1;
    
    begin
      int_array := typ_int_g();
      --str := norm(v_str); /* normalize string excluding symbols */
      str := v_str;
      while pos > 0 loop
        
        pos := instr(str, ' ');
        str := substr(str, pos+1); -- added this
    
        if pos > 0 then
          int_array.extend; -- moved it in IF 
          int_array(i) := rec_int_g(to_number(trim(substr(str, 1, pos)))); -- main change
          i := i+1; -- added this -- incremented the index value
        end if;
    
     end loop;
    DBMS_OUTPUT.PUT_LINE('iteration count: ' || i); -- output for testing
    end iparse;
    /
    

    Check the DB<>Fiddle demo.