The ORA-06530: Reference to uninitialized composite error occurs when an object, LOB, or other composite was referenced as a left hand side without having been initialized. When a reference to an uninitialised object, LOB or composite is used to assign or access a value, the error ORA-06530: Reference to uninitialized composite occurs. The variable is not initialized with an oracle object or composite object. The variable may be used to assign a value or to access a value. The variable can’t be used if it hasn’t been assigned anything.

when a variable is created using an oracle data type, the variable is initialized and memory to storing values is created. A composite variable is created and uninitialized when a custom structure is created in Oracle. An explicit initialization of the variable is required. If the variable is used without being initialised, the reference to the uninitialised composite variable creates an ORA-06530: Reference to uninitialized composite error.

A value cannot be stored in an uninitialized variable because it requires memory. If the reference to the uninitialized variable attempts to retrieve or store the value from memory, the value will not be available. Since the memory for the variable has not been created, the reference to the variable would result in an error. If you create a variable with oracle basic data types like number, varchar, varchar2, char, and so on, you won’t have this problem.



Problem

An oracle type object is created. The name of the type object is Student. A student variable is declared in the PL/SQL statement. Values such as id and name are assigned to the student variable. The error would be thrown if you run the example below. The student variable attributes such as id and name are used without creating the student object and initialised.

create or replace type student is object
(
id numeric(5),
name varchar2(100)
);
declare
 st student;
begin
    st.id:=1;
    st.name:='yawin';
end;


ORA-06530 Error

The error is thrown if the student variable is used as a reference to assign a value to a type member.The student variable is not initialised with an oracle object or composite. The error will be shown as follows. The student variable is not initialised with a student object. The student variable was referenced as a left hand side without having been initialised.

declare
 st student;
begin
    st.id:=1;
    st.name:='yawin';
end;
Error report -
ORA-06530: Reference to uninitialized composite
ORA-06512: at line 4
06530. 00000 -  "Reference to uninitialized composite"


Cause

An object, LOB, or other composite was referenced as a left hand side without having been initialized.



Action

Initialize the composite with an appropriate constructor or whole-object assignment.



Solution 1

The constructor method should be used to initialize the uninitialized variable. The oracle composite constructor method can be used to create an oracle object, which can then be initialized to a variable. The student object should be created and assigned to the student variable using the student constructor method. In the example below, the constructor method is used to create the student object by passing all of the member variable values. The student object is initialized using the student variable.

declare
 st student;
begin
    st := student(1,'yawin');
end;

Output

PL/SQL procedure successfully completed.


Solution 2

The uninitiated variable should be assigned with a oracle object that is created using the reference object attribute by their names. In this case, the attribute’s order can be changed. A value should be assigned to each attribute. The variable name is used to name each value. As a result, the name helps in mapping the value.

declare
 st student;
begin
    st := student(id=>1,name=>'yawin');
end;

Output

PL/SQL procedure successfully completed.


Solution 3

If the oracle object attribute values are specified at runtime, the oracle object or composite should be created with a constructor that accepts null as an attribute value. If any of the attribute is configured with not null, you can not use this method.After creating the oracle object, the value can be assigned.

declare
 st student;
begin
    st := student(null,null);
    st.id:=1;
    st.name:='yawin';
end;

Output

PL/SQL procedure successfully completed.



Leave a Reply