IBM i message

RNQ0121: Array index not valid

RNQ0121 means an ILE RPG program used an array index that is zero, negative or larger than the number of elements in the array. It matches RPG program status code 00121, and the job is waiting for a reply.

Other names for this code

Also written as RNX0121, status 00121, RPG status 121, %STATUS 121, RPG array index error, index for array not valid.

What it means

RNQ0121 is an ILE RPG runtime message for program status 00121, which IBM's ILE RPG Reference lists as an array index that isn't valid. RPG arrays start at element 1 and end at the number given in DIM. Every time the program reads or changes an element, the runtime checks the index. An index of 0, a negative index, or one above the DIM value raises the error. The help text names the array.

A multiple-occurrence data structure has its own status. Setting an occurrence outside its range is status 00122, not 00121.

RNQ0121 is the inquiry form of the message. The RPG default handler sends it when the program had no MONITOR group, *PSSR subroutine or other handler for the error, and the job stops until someone replies. In a batch job the question usually goes to QSYSOPR. RNX0121 is the escape form with the same meaning. It doesn't wait for a reply and is what a MONITOR group or a calling program sees.

Common causes

  • A %LOOKUP or LOOKUP that found nothing. %LOOKUP returns 0 in that case, and using the result directly as an index fails.
  • A loop that runs one step too far, for example FOR i = 1 TO count when count is larger than the array.
  • More data than the array was sized for, such as 60 lines loaded into an array declared with DIM(50).
  • An index taken from the data, like a month number of 0 or 13, or a code that maps outside the table.
  • An index variable that was never set and is still zero.

How to find the problem

  1. Press F1 on the message. The help text names the array, the procedure, the program and the statement number. For a batch job, look in QSYSOPR (DSPMSG QSYSOPR) or the job log (DSPJOBLOG).
  2. Find the statement in the compile listing and work out which expression is the index.
  3. Reply D for a formatted dump, which shows the index variable's value when the program failed. Or rerun under STRDBG with a breakpoint on that statement.
  4. Compare the index value with the array's DIM. A value of 0 usually points to a failed lookup or an unset counter. A value one above DIM points to a loop bound.

How to fix it

  • Check the index before using it. It must be at least 1 and no more than %ELEM(array).
  • Test the result of %LOOKUP for 0 before using it as an index.
  • Use %ELEM instead of a hard-coded number in loop limits, so the loop follows the array if DIM changes.
  • If the data really can exceed the array, raise DIM or stop loading when the array is full and report it.
  • Reply C for the waiting job, then fix the program and rerun.

IBM documentation

IBM Display Message Descriptions (DSPMSGD). Use the documentation for your installed release.

This link opens the relevant IBM reference library or lookup instructions. Match the complete code and your installed product release; it is not a code-specific review.

Report a correction to WND Software.

Questions

Do RPG arrays start at 0 or 1?

At 1. The first element is array(1) and the last is array(n), where n is the DIM value. An index of 0 always gives status 00121.

What is the difference between RNQ0121 and RNX0121?

They report the same error. RNQ0121 is an inquiry that waits for a reply because the program didn't handle it. RNX0121 is the escape form that MONITOR or a calling program can catch.

Which reply should I give to an RNQ message?

C (cancel) is the safe answer when you don't know the program. D and F also cancel, but first write a dump the developers can use. G skips the rest of the current RPG cycle and resumes at its input step (*GETIN). That is a call for the program's owner to make.