donnemartin/interactive-coding-challenges · error · TypeError
matrix and val cannot be None
Error message
matrix and val cannot be None
What it means
SortedMatrix.find_val raises TypeError('matrix and val cannot be None') when either the matrix or the searched value is None. The staircase search immediately indexes matrix[0], so a None matrix would crash with TypeError anyway; the guard also rejects None targets since equality comparison against None is meaningless here.
Source
Thrown at sorting_searching/search_sorted_matrix/search_sorted_matrix_solution.ipynb:117
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class SortedMatrix(object):\n",
"\n",
" def find_val(self, matrix, val):\n",
" if matrix is None or val is None:\n",
" raise TypeError('matrix and val cannot be None')\n",
" row = 0\n",
" col = len(matrix[0]) - 1\n",
" while row < len(matrix) and col >= 0:\n",
" if matrix[row][col] == val:\n",
" return (row, col)\n",
" elif matrix[row][col] > val:\n",
" col -= 1\n",
" else:\n",
" row += 1\n",
" return None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]View on GitHub (pinned to 358f2cc604)
Solutions
- Validate that both matrix and val are non-None before calling find_val
- Trace where val is produced and supply a default or fail early if missing
- Also verify the matrix is non-empty before calling, to avoid the separate IndexError on matrix[0]
Example fix
// before pos = SortedMatrix().find_val(matrix, val) # val may be None // after pos = SortedMatrix().find_val(matrix, val) if (matrix and val is not None) else None
Defensive patterns
Strategy: validation
Validate before calling
if matrix is None or val is None or not matrix or not matrix[0]:
return None
return SortedMatrix().find_val(matrix, val) Type guard
def is_searchable_matrix(m):
return (isinstance(m, list) and len(m) > 0
and isinstance(m[0], list) and m[0]) Try / catch
try:
pos = sm.find_val(matrix, val)
except TypeError as e:
if 'cannot be None' in str(e):
pos = None
else:
raise Prevention
- Validate matrix loading succeeded (non-None, non-empty)
- Default missing search values instead of passing None
- Check matrix[0] exists to avoid the separate IndexError
When it happens
Trigger: Calling find_val(None, x) or find_val(matrix, None); also when matrix is an empty list the code raises IndexError rather than this error, so this error specifically means one argument was None.
Common situations: Search targets sourced from optional request parameters or dict lookups that returned None; matrices loaded from files/APIs that failed and defaulted to None.
Related errors
- array cannot be None
- sentence cannot be None
- rows and cols cannot be None
- s or t cannot be None
- a or b cannot be None
AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28).
Data as JSON: /api/errors/bb8ce746d20be899.
Report an issue: GitHub.