How to Fix ORA-06530: Reference to Uninitialized Composite in Oracle

Initialize object type variables with a constructor before setting their attributes, and check for NULL objects passed in.

ORA-06530 is raised when PL/SQL code sets or reads an attribute of an object type variable that is still NULL. Declaring a variable of an object type does not create an object; until a constructor or a query assigns one, the variable is atomically null and has no attributes to set. The predefined exception is ACCESS_INTO_NULL. Call the constructor first.

Reproduce the Error

Example:

create type demo_point_t as object (x number, y number);
/
declare
  p demo_point_t;
begin
  p.x := 10;
end;
/

Output:

Type DEMO_POINT_T compiled

declare
*
ERROR at line 1:
ORA-06530: Reference to uninitialized composite
ORA-06512: at line 4

Fix: Initialize with the Constructor

Example:

set serveroutput on
declare
  p demo_point_t := demo_point_t(null, null);    -- constructor first
begin
  p.x := 10;
  p.y := 20;
  dbms_output.put_line(p.x || ', ' || p.y);
  dbms_output.put_line(case when p is null then 'null' else 'initialized' end);
end;
/

Output:

10, 20
initialized

PL/SQL procedure successfully completed.

The constructor DEMO_POINT_T(NULL, NULL) creates an object whose attributes are NULL, but the object itself is not, so its attributes can be set.

Things to Know

  • A SELECT INTO of an object column, or a function returning the type, also initializes the variable.
  • Test the object with IS NULL before using it when it may not be set, for example a parameter passed as NULL.
  • Records declared with %ROWTYPE or TYPE ... IS RECORD do not need a constructor.

Related Guides

Conclusion

ORA-06530 means an object variable was used before it held an object. Assign it with the type's constructor, a query, or a function before setting attributes, and test IS NULL when it may be missing.

Get more Vinish.dev tutorials on Google

See my articles more often when you search Google.

Vinish Kapoor
Vinish Kapoor

An Oracle ACE, author of four books on Oracle APEX, SQL and PL/SQL, and Oracle Forms, and a software developer building Oracle database applications since 2001.

guest

0 Comments
Oldest
Newest Most Voted