Q:

How to initialize Type of Record and overcome ORA-06530: Reference to uninitialized composite error

I want to initialize the values into the RECORD TYPE. Below is my code.
I am facing "ORA-06530: Reference to uninitialized composite error" at runtime. The code compiles fine.
CREATE OR REPLACE TYPE TY_PERSONAL_MASTER AS OBJECT
(
ID      NUMBER(20),
FNAME   VARCHAR2(30),
LNAME   VARCHAR2(30)
);

CREATE OR REPLACE TYPE TY_PROF_MASTERS IS TABLE OF TY_PROF_MASTER;

CREATE OR REPLACE TYPE TY_PROF_MASTER AS OBJECT 
(
ID        NUMBER(20),
EMPLOYER  VARCHAR2(20),
DOJ       DATE
);

CREATE TABLE TABLE_HOBBIES
(
ID      NUMBER(20),
HNAME   VARCHAR2(20)
);

CREATE OR REPLACE PACKAGE INITIALIZE IS

TYPE PERSON IS RECORD(
PERSONAL        TY_PERSONAL_MASTER,
PROFESSIONAL    TY_PROF_MASTERS,
v_hobbies       TABLE_HOBBIES%ROWTYE
)

END INITIALIZE;

CREATE OR REPLACE PACKAGE BODY INITIALIZE IS

FUNCTION FN_INITIALIZE(P_ACTION IN VARCHAR2) RETURN BOOLEAN IS
L_TO_INITIALIZE PERSON;

BEGIN
L_TO_INITIALIZE.PERSONAL.ID := 1;
-- The above line throws "ORA-06530: Reference to uninitialized composite error" at runtime.
-- I have just given example of assigning 1 value. However, I want to initialize all the values. 

END FN_INITIALIZE;
END INITIALIZE;

A:

They are different types so you have to handle them differently:
CREATE OR REPLACE PACKAGE BODY INITIALIZE IS

  FUNCTION FN_INITIALIZE(P_ACTION IN VARCHAR2) RETURN BOOLEAN IS
    L_TO_INITIALIZE PERSON;
  BEGIN
    -- object so need to call constructor
    L_TO_INITIALIZE.PERSONAL := TY_PERSONAL_MASTER(1, 'Bob', 'Hope');
    -- can also assign values directly once created
    L_TO_INITIALIZE.PERSONAL.ID := 1;
    L_TO_INITIALIZE.PERSONAL.FNAME := 'Bob';
    L_TO_INITIALIZE.PERSONAL.LNAME := 'Hope';

    -- table type so need to call constructor
    L_TO_INITIALIZE.PROFESSIONAL := new TY_PROF_MASTERS();
    -- need to extend to make room for new entry
    L_TO_INITIALIZE.PROFESSIONAL.extend(1);
    -- object so need to call constructor
    L_TO_INITIALIZE.PROFESSIONAL(1) := new TY_PROF_MASTER(2, 'USO', date '1941-05-06');
    -- can also assign values directly once created
    L_TO_INITIALIZE.PROFESSIONAL(1).DOJ := date '1941-03-01';

    -- record so no constructor, just assign values
    L_TO_INITIALIZE.v_hobbies.ID := 3;
    L_TO_INITIALIZE.v_hobbies.HNAME := 'Fishing';

    RETURN TRUE;
  END FN_INITIALIZE;
END INITIALIZE;
/

