Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-25 · container image gvenzl/oracle-free:23-slim
A nested table or VARRAY variable declared without a value is NULL, not empty. Calling EXTEND, reading COUNT or using v(1) on it raises ORA-06531. It becomes usable only after a constructor such as name_tab() gives it a value (an empty collection with COUNT 0).
ORA-06512: at line N points to the line in the block that touched the NULL collection, counting DECLARE as line 1.
01Collection declared without a constructor, then EXTEND
v_names name_tab; was declared with no initial value, and the loop called v_names.EXTEND first. That line (line 6) failed with ORA-06531 on the very first row.
The check shows the difference: v1 name_tab; is NULL (TRUE), while v2 name_tab := name_tab(); is not NULL and has count = 0. Declaring v_names name_tab := name_tab(); loaded all rows (count = 3).
Setup
CREATE TABLE emp (emp_id NUMBER, name VARCHAR2(20));
INSERT INTO emp VALUES (1, 'KIM');
INSERT INTO emp VALUES (2, 'LEE');
INSERT INTO emp VALUES (3, 'PARK');
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
Commit complete.SET SERVEROUTPUT ON
DECLARE
TYPE name_tab IS TABLE OF VARCHAR2(20);
v_names name_tab;
BEGIN
FOR r IN (SELECT name FROM emp ORDER BY emp_id) LOOP
v_names.EXTEND;
v_names(v_names.LAST) := r.name;
END LOOP;
DBMS_OUTPUT.PUT_LINE('count = ' || v_names.COUNT);
END;
/DECLARE
*
ERROR at line 1:
ORA-06531: Reference to uninitialized collection
ORA-06512: at line 6
ORA-06512: at line 6
Help: https://docs.oracle.com/error-help/db/ora-06531/SET SERVEROUTPUT ON
DECLARE
TYPE name_tab IS TABLE OF VARCHAR2(20);
v1 name_tab;
v2 name_tab := name_tab();
BEGIN
DBMS_OUTPUT.PUT_LINE('v1 IS NULL: ' || CASE WHEN v1 IS NULL THEN 'TRUE' ELSE 'FALSE' END);
DBMS_OUTPUT.PUT_LINE('v2 IS NULL: ' || CASE WHEN v2 IS NULL THEN 'TRUE' ELSE 'FALSE' END || ', count = ' || v2.COUNT);
END;
/v1 IS NULL: TRUE
v2 IS NULL: FALSE, count = 0
PL/SQL procedure successfully completed.name_tab() with no arguments creates an empty collection. Put it in the declaration so the variable is never NULL.
SET SERVEROUTPUT ON
DECLARE
TYPE name_tab IS TABLE OF VARCHAR2(20);
v_names name_tab := name_tab();
BEGIN
FOR r IN (SELECT name FROM emp ORDER BY emp_id) LOOP
v_names.EXTEND;
v_names(v_names.LAST) := r.name;
END LOOP;
DBMS_OUTPUT.PUT_LINE('count = ' || v_names.COUNT);
END;
/count = 3
PL/SQL procedure successfully completed.02Function returns a NULL collection when there are no rows
GET_ORDERS created the collection only inside the loop, the first time a row was found. Customer 1 has orders, so the caller printed customer 1: 2. Customer 2 has none, so the loop never ran and the function returned NULL. Reading v.COUNT in the caller failed at line 7.
Initializing the variable in the function (v_ids id_list := id_list();) makes it return an empty collection, and the same caller printed customer 2: 0.
Setup
CREATE TABLE orders (order_id NUMBER, cust_id NUMBER, amount NUMBER);
INSERT INTO orders VALUES (1001, 1, 500);
INSERT INTO orders VALUES (1002, 1, 300);
COMMIT;
CREATE TYPE id_list AS TABLE OF NUMBER;
/
CREATE OR REPLACE FUNCTION get_orders (p_cust NUMBER) RETURN id_list IS
v_ids id_list;
BEGIN
FOR r IN (SELECT order_id FROM orders WHERE cust_id = p_cust ORDER BY order_id) LOOP
IF v_ids IS NULL THEN
v_ids := id_list();
END IF;
v_ids.EXTEND;
v_ids(v_ids.LAST) := r.order_id;
END LOOP;
RETURN v_ids;
END;
/Table created.
1 row created.
1 row created.
Commit complete.
Type created.
Function created.SET SERVEROUTPUT ON
DECLARE
v id_list;
BEGIN
v := get_orders(1);
DBMS_OUTPUT.PUT_LINE('customer 1: ' || v.COUNT);
v := get_orders(2);
DBMS_OUTPUT.PUT_LINE('customer 2: ' || v.COUNT);
END;
/customer 1: 2
DECLARE
*
ERROR at line 1:
ORA-06531: Reference to uninitialized collection
ORA-06512: at line 7
Help: https://docs.oracle.com/error-help/db/ora-06531/CREATE OR REPLACE FUNCTION get_orders (p_cust NUMBER) RETURN id_list IS
v_ids id_list := id_list();
BEGIN
FOR r IN (SELECT order_id FROM orders WHERE cust_id = p_cust ORDER BY order_id) LOOP
v_ids.EXTEND;
v_ids(v_ids.LAST) := r.order_id;
END LOOP;
RETURN v_ids;
END;
/Function created.A function that returns a collection should return an empty one, not NULL, when nothing was found. Then callers can use COUNT safely.
SET SERVEROUTPUT ON
DECLARE
v id_list;
BEGIN
v := get_orders(1);
DBMS_OUTPUT.PUT_LINE('customer 1: ' || v.COUNT);
v := get_orders(2);
DBMS_OUTPUT.PUT_LINE('customer 2: ' || v.COUNT);
END;
/customer 1: 2
customer 2: 0
PL/SQL procedure successfully completed.03Object type created with NULL for its collection attribute
ORDER_T has a nested table attribute items. The object was created as order_t(1001, NULL), so v.items was NULL (the check printed items IS NULL: TRUE). v.items.EXTEND failed at line 4. The object itself was initialized, but its collection attribute was not.
Passing an empty collection in the constructor, order_t(1001, item_list()), let EXTEND and the assignment work (items = 1).
Setup
CREATE TYPE item_list AS TABLE OF VARCHAR2(20);
/
CREATE TYPE order_t AS OBJECT (order_id NUMBER, items item_list);
/Type created.
Type created.SET SERVEROUTPUT ON
DECLARE
v order_t := order_t(1001, NULL);
BEGIN
v.items.EXTEND;
v.items(1) := 'PEN';
DBMS_OUTPUT.PUT_LINE('items = ' || v.items.COUNT);
END;
/DECLARE
*
ERROR at line 1:
ORA-06531: Reference to uninitialized collection
ORA-06512: at line 4
Help: https://docs.oracle.com/error-help/db/ora-06531/SET SERVEROUTPUT ON
DECLARE
v order_t := order_t(1001, NULL);
BEGIN
DBMS_OUTPUT.PUT_LINE('items IS NULL: ' || CASE WHEN v.items IS NULL THEN 'TRUE' ELSE 'FALSE' END);
END;
/items IS NULL: TRUE
PL/SQL procedure successfully completed.Initializing an object does not initialize the collections inside it. Pass item_list() (or a filled list) for each collection attribute.
SET SERVEROUTPUT ON
DECLARE
v order_t := order_t(1001, item_list());
BEGIN
v.items.EXTEND;
v.items(1) := 'PEN';
DBMS_OUTPUT.PUT_LINE('items = ' || v.items.COUNT);
END;
/items = 1
PL/SQL procedure successfully completed.FAQ
What is the difference between ORA-06531 and ORA-06533?
ORA-06531: the collection itself is NULL (no constructor yet). ORA-06533: the collection exists but the index is larger than its COUNT. After v := t();, v(1) gives ORA-06533, not ORA-06531.
Does COUNT on an uninitialized collection return 0?
No. In the test, v.COUNT on a NULL collection returned by a function raised ORA-06531. COUNT is 0 only after a constructor like id_list().
How do I check if a collection is initialized?
Use v IS NULL. In the test, a variable declared without a value was TRUE and one declared with name_tab() was FALSE.
Why do I get ORA-06531 when the object was initialized?
The object can be initialized while its collection attribute is still NULL, as with order_t(1001, NULL) in the test. Pass item_list() for the attribute.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0