Goos van den Bekerom
Goos van den Bekerom

Reputation: 1493

PL/SQL function stops running after loop

I feel like this code has been working all day but all of a sudden it doesn't work anymore and I can't seem to find the solution.

This Loop is inside a big function so I'll only post this:

FOR i in 0..t_strGroups.count
LOOP
  newStudentNumber := newStudentNumber || t_strGroups(i) || ' ';
  dbms_output.put_line(newStudentNumber); -- this outputs fine
END LOOP;
dbms_output.put_line('test'); --this line doesn't output

When testing where the function stopped working I found that after this loop I can no longer output lines. Does anyone know what might have happened here?

Thanks in advance!

Upvotes: 2

Views: 724

Answers (1)

Goos van den Bekerom
Goos van den Bekerom

Reputation: 1493

After thinking a bit more about this I found the answer.

In the loop I loop from 0 to t_strGroups.count. Because the first position of my collection is 0. but collection.count returns the ammount of values in the collection.

I have 5 values in my collection so collection.count returns 5. however, the last value in my collection has position 4. So when the loops gets to itteration 5 the code will crash. Since position 5 does not exist.

I guess it is just stupid of mine to overlook this. And I thought about removing this question. But I decided to post this answer. Maybe it can help someone some day!

EDIT: According to the comment of Jeffrey Kemp the code doesn't crash it raises a NO_DATA_FOUND Exception. So it would also be smart to write an exception handler for that.

Upvotes: 2

Related Questions