{"record":{"id":"bb8ce746d20be899","repo":"donnemartin/interactive-coding-challenges","slug":"matrix-and-val-cannot-be-none","errorCode":null,"errorMessage":"matrix and val cannot be None","messagePattern":"matrix and val cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sorting_searching/search_sorted_matrix/search_sorted_matrix_solution.ipynb","lineNumber":117,"sourceCode":"  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class SortedMatrix(object):\\n\",\n    \"\\n\",\n    \"    def find_val(self, matrix, val):\\n\",\n    \"        if matrix is None or val is None:\\n\",\n    \"            raise TypeError('matrix and val cannot be None')\\n\",\n    \"        row = 0\\n\",\n    \"        col = len(matrix[0]) - 1\\n\",\n    \"        while row < len(matrix) and col >= 0:\\n\",\n    \"            if matrix[row][col] == val:\\n\",\n    \"                return (row, col)\\n\",\n    \"            elif matrix[row][col] > val:\\n\",\n    \"                col -= 1\\n\",\n    \"            else:\\n\",\n    \"                row += 1\\n\",\n    \"        return None\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/sorting_searching/search_sorted_matrix/search_sorted_matrix_solution.ipynb#L99-L135","documentation":"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.","triggerScenarios":"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.","commonSituations":"Search targets sourced from optional request parameters or dict lookups that returned None; matrices loaded from files/APIs that failed and defaulted to None.","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]"],"exampleFix":"// before\npos = SortedMatrix().find_val(matrix, val)  # val may be None\n\n// after\npos = SortedMatrix().find_val(matrix, val) if (matrix and val is not None) else None","handlingStrategy":"validation","validationCode":"if matrix is None or val is None or not matrix or not matrix[0]:\n    return None\nreturn SortedMatrix().find_val(matrix, val)","typeGuard":"def is_searchable_matrix(m):\n    return (isinstance(m, list) and len(m) > 0\n            and isinstance(m[0], list) and m[0])","tryCatchPattern":"try:\n    pos = sm.find_val(matrix, val)\nexcept TypeError as e:\n    if 'cannot be None' in str(e):\n        pos = None\n    else:\n        raise","preventionTips":["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"],"tags":["python","input-validation","typeerror","matrix-search"],"backgroundTag":"none-input-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}