arraysoracle-databasetypesplsqloracle-type

Oracle object array initialization collection


I am trying to create an array of oracle object type and I am getting some initialization related error.

Below is the sample code

CREATE OR REPLACE TYPE rectangle AS OBJECT
(
-- The type has 3 attributes.
  length NUMBER,
  width NUMBER,
  area NUMBER,
-- Define a constructor that has only 2 parameters.
  CONSTRUCTOR FUNCTION rectangle(length NUMBER, width NUMBER)
    RETURN SELF AS RESULT
);

CREATE OR REPLACE TYPE BODY rectangle AS
  CONSTRUCTOR FUNCTION rectangle(length NUMBER, width NUMBER)
    RETURN SELF AS RESULT
  AS
  BEGIN
    SELF.length := length;
    SELF.width := width;

    SELF.area := length * width;
    RETURN;
  END;
END;

To run below is the sample script

set serveroutput on
DECLARE
  r1 rectangle;
  r2 rectangle;
  type rect_arr is table of rectangle;
  m_rect  rect_arr;
BEGIN
  m_rect.extend;
  m_rect(1) := rectangle(10,20);
  sop(m_rect(1).area);  
END;

Below is the error on console----- I know it is related to initialization error, and tried various constructor and member function solutions but none help.

Error report: ORA-06531: Reference to uninitialized collection ORA-06512: at line 7 06531. 00000 - "Reference to uninitialized collection" *Cause: An element or member function of a nested table or varray was referenced (where an initialized collection is needed) without the collection having been initialized. *Action: Initialize the collection with an appropriate constructor or whole-object assignment.


Solution

  • m_rect rect_arr := rect_arr();