Error Reference Hub
NV4002Runtime
Index Out of Bounds
An indexing operation requested an element index outside the valid range [0, length - 1] of a list, array, or string.
Why this error occurs
Accessing index >= len(list) or negative index exceeding -len(list), or indexing an empty collection.
Incorrect Code
let numbers = [10, 20, 30]
let item = numbers[5] // runtime error: index 5 out of bounds for length 3Corrected Code
let numbers = [10, 20, 30]
let idx = 5
if idx < len(numbers):
print(numbers[idx])
else:
print("Index out of range")Troubleshooting & Remediation
- 1Check 'len(collection)' before accessing fixed indices.
- 2Prefer 'for item in collection:' loops over manual index arithmetic.
- 3Verify loop boundary conditions ('< len' instead of '<= len').
Relevant CLI Commands
$ nextviper run <file.nv>$ nextviper test
