TheAlgorithms/JavaScript · error · TypeError
Grid must be a non-empty array
Error message
Grid must be a non-empty array
What it means
Thrown by validateGrid() in the Rat-in-a-Maze solver before any backtracking begins. The solver indexes grid[y][x], so it requires an actual Array with at least one row; null, undefined, objects, primitives, or an empty array are all rejected up front.
Source
Thrown at Backtracking/RatInAMaze.js:26
* Reference for this problem: https://www.geeksforgeeks.org/rat-in-a-maze-backtracking-2/
*
* Based on the original implementation contributed by Chiranjeev Thapliyal (https://github.com/chiranjeev-thapliyal).
*/
/**
* Checks if the given grid is valid.
*
* A grid needs to satisfy these conditions:
* - must not be empty
* - must be a square
* - must not contain values other than {@code 0} and {@code 1}
*
* @param grid The grid to check.
* @throws TypeError When the given grid is invalid.
*/
function validateGrid(grid) {
if (!Array.isArray(grid) || grid.length === 0)
throw new TypeError('Grid must be a non-empty array')
const allRowsHaveCorrectLength = grid.every(
(row) => row.length === grid.length
)
if (!allRowsHaveCorrectLength) throw new TypeError('Grid must be a square')
const allCellsHaveValidValues = grid.every((row) => {
return row.every((cell) => cell === 0 || cell === 1)
})
if (!allCellsHaveValidValues)
throw new TypeError('Grid must only contain 0s and 1s')
}
function isSafe(grid, x, y) {
const n = grid.length
return x >= 0 && x < n && y >= 0 && y < n && grid[y][x] === 1
}
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Pass a non-empty square 2D array of 0/1 values, e.g. [[1,0,1],[0,1,0],[0,0,1]].
- If the grid comes from JSON or a fetch, verify Array.isArray(grid) && grid.length>0 before calling the solver.
- Ensure the variable is not shadowed or accidentally set to undefined by a failed map/assignment.
Example fix
// before const path = solveRatInAMaze(parsed ?? []) // after const path = Array.isArray(parsed) && parsed.length ? solveRatInAMaze(parsed) : null
Defensive patterns
Strategy: validation
Validate before calling
function isValidMazeGrid(g) {
if (!Array.isArray(g) || g.length === 0) return false;
const n = g.length;
return g.every(r => Array.isArray(r) && r.length === n && r.every(c => c === 0 || c === 1));
}
// if (isValidMazeGrid(grid)) solve(grid); Type guard
/** @param {unknown} g @returns {g is number[][]} */
function isSquareBinaryGrid(g) {
if (!Array.isArray(g) || g.length === 0) return false;
const n = g.length;
return g.every(r => Array.isArray(r) && r.length === n &&
r.every(c => c === 0 || c === 1));
} Try / catch
try { solve(grid); }
catch (e) {
if (e instanceof TypeError && /non-empty array/.test(e.message)) {
// handle bad/missing grid
} else throw e;
} Prevention
- Validate grid shape and cell values at the data-loading boundary, not at solve time.
- Parse maze text into numbers explicitly: row.split('').map(Number).
- Keep visited/state separate from the source grid to avoid needing values beyond 0/1.
When it happens
Trigger: Calling solve()/validateGrid with null, undefined, [], {}, a number, a string, a Set, or any non-Array value. Also triggered when a JSON parse returns null/empty and that result is passed straight through.
Common situations: Loading the maze from a JSON config that failed to parse, defaulting an unset option to [] (empty), or passing a matrix-library wrapper / typed array instead of a plain Array of Arrays.
Related errors
- Grid must be a square
- Grid must only contain 0s and 1s
- LFUCache ERROR: The Capacity is 0
- Both keyword and message must be specified
- Invalid keyword!
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/42f2fb32e41f2365.
Report an issue: GitHub.