DEV Community

umzzil nng
umzzil nng

Posted on Originally published at oraerror.com

Oracle ORA-06530 Error: Causes and Solutions Complete Guide

ORA-06530: Reference to Uninitialized Composite — What It Means and How to Fix It

ORA-06530 is a PL/SQL runtime error that occurs when you attempt to access an attribute or call a method on a composite type variable — such as a user-defined Object Type, Nested Table, or VARRAY — that has been declared but never initialized. In Oracle, simply declaring a composite variable does not allocate an instance; you must explicitly call its constructor to bring it to life. This error is one of the most common pitfalls when working with Oracle's object-oriented PL/SQL features.


Top 3 Causes

1. Accessing an Object Type Variable Without Calling Its Constructor

The most frequent cause: a developer declares an object type variable and immediately tries to set its attributes, forgetting that the variable is NULL by default.

-- ERROR: Object declared but not initialized
CREATE OR REPLACE TYPE product_obj AS OBJECT (
    product_id   NUMBER,
    product_name VARCHAR2(200)
);
/

DECLARE
    v_product product_obj;   -- NULL at this point
BEGIN
    v_product.product_id := 101;  -- ORA-06530 raised here!
END;
/

-- FIX: Call the constructor first
DECLARE
    v_product product_obj;
BEGIN
    v_product := product_obj(101, 'Oracle Database');  -- Initialize
    v_product.product_name := 'Oracle Database 19c';  -- Now safe
    DBMS_OUTPUT.PUT_LINE(v_product.product_name);
END;
/
Enter fullscreen mode Exit fullscreen mode

2. Using a Collection Without Initialization

Nested Tables and VARRAYs must be initialized with their constructor before you can call any collection method (EXTEND, COUNT, FIRST, LAST) or access any element by index.

-- ERROR: Collection not initialized
CREATE OR REPLACE TYPE str_list AS TABLE OF VARCHAR2(100);
/

DECLARE
    v_items str_list;   -- NULL, not an empty collection
BEGIN
    v_items.EXTEND;     -- ORA-06530 raised here!
    v_items(1) := 'First Item';
END;
/

-- FIX: Initialize with an empty or pre-populated constructor
DECLARE
    v_items str_list;
BEGIN
    v_items := str_list();          -- Empty collection
    v_items.EXTEND(3);
    v_items(1) := 'Alpha';
    v_items(2) := 'Beta';
    v_items(3) := 'Gamma';

    FOR i IN v_items.FIRST .. v_items.LAST LOOP
        DBMS_OUTPUT.PUT_LINE(v_items(i));
    END LOOP;
END;
/
Enter fullscreen mode Exit fullscreen mode

3. Uninitialized OUT Parameter in a Procedure

When passing a composite type as an OUT parameter, both the caller and the procedure body must ensure the object is initialized before any attribute is assigned inside the procedure.

-- ERROR: OUT parameter used without initialization inside the procedure
CREATE OR REPLACE PROCEDURE fetch_product (
    p_id      IN  NUMBER,
    p_product OUT product_obj
) IS
BEGIN
    p_product.product_id := p_id;  -- ORA-06530! p_product is still NULL
END;
/

-- FIX: Initialize inside the procedure before use
CREATE OR REPLACE PROCEDURE fetch_product (
    p_id      IN  NUMBER,
    p_product OUT product_obj
) IS
BEGIN
    p_product := product_obj(NULL, NULL);   -- Initialize first
    p_product.product_id   := p_id;
    p_product.product_name := 'Sample Product';
END;
/

-- Caller
DECLARE
    v_prod product_obj;
BEGIN
    fetch_product(55, v_prod);
    DBMS_OUTPUT.PUT_LINE('ID: ' || v_prod.product_id);
END;
/
Enter fullscreen mode Exit fullscreen mode

Quick Fix Solutions

Scenario Fix
Object type variable is NULL Call v_obj := MyType(NULL, NULL); before use
Empty collection needed Call v_col := MyCollType(); before EXTEND
OUT param composite Initialize inside the procedure with a constructor
Uncertain if initialized Use IF v_obj IS NULL THEN ... END IF; guard

Prevention Tips

1. Always initialize at declaration time.
Make it a team coding standard to initialize every composite variable right at the point of declaration in the DECLARE block. This eliminates the entire class of uninitialized-access errors before the code even runs.

DECLARE
    -- Initialize at declaration — always safe
    v_product product_obj := product_obj(NULL, NULL);
    v_items   str_list    := str_list();
BEGIN
    v_product.product_id   := 200;
    v_product.product_name := 'Best Practice Product';
    v_items.EXTEND;
    v_items(1) := 'Initialized Item';
    DBMS_OUTPUT.PUT_LINE(v_product.product_name || ' | ' || v_items(1));
END;
/
Enter fullscreen mode Exit fullscreen mode

2. Add NULL guards and explicit exception handling.
For critical code paths, add IS NULL checks before accessing composite variables and handle ORA-06530 explicitly in your EXCEPTION block to produce meaningful error messages instead of cryptic runtime failures.

DECLARE
    v_product product_obj;
BEGIN
    -- Defensive NULL check before access
    IF v_product IS NULL THEN
        v_product := product_obj(0, 'DEFAULT');
    END IF;
    DBMS_OUTPUT.PUT_LINE('Product: ' || v_product.product_name);
EXCEPTION
    WHEN OTHERS THEN
        IF SQLCODE = -6530 THEN
            DBMS_OUTPUT.PUT_LINE('Composite type was not initialized. Please review the code.');
        ELSE
            RAISE;
        END IF;
END;
/
Enter fullscreen mode Exit fullscreen mode

Related Errors

  • ORA-06531Reference to uninitialized collection: Specifically raised for uninitialized collection types; closely related to ORA-06530.
  • ORA-06532Subscript outside of limit: VARRAY index exceeds the declared maximum size.
  • ORA-06533Subscript beyond count: Index exceeds the current number of elements in a collection.

📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.

Top comments (0)