{"record":{"id":"a3346f8084414aa1","repo":"donnemartin/interactive-coding-challenges","slug":"grid-cannot-be-none","errorCode":null,"errorMessage":"grid cannot be None","messagePattern":"grid cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"online_judges/island_perimeter/island_perimeter_solution.ipynb","lineNumber":105,"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 Solution(object):\\n\",\n    \"\\n\",\n    \"    def island_perimeter(self, grid):\\n\",\n    \"        if grid is None:\\n\",\n    \"            raise TypeError('grid cannot be None')\\n\",\n    \"        sides = 0\\n\",\n    \"        num_rows = len(grid)\\n\",\n    \"        num_cols = len(grid[0])\\n\",\n    \"        for i in range(num_rows):\\n\",\n    \"            for j in range(num_cols):\\n\",\n    \"                if grid[i][j] == 1:\\n\",\n    \"                    # Check left\\n\",\n    \"                    if j == 0 or grid[i][j - 1] == 0:\\n\",\n    \"                        sides += 1\\n\",\n    \"                    # Check right\\n\",\n    \"                    if j == num_cols - 1 or grid[i][j + 1] == 0:\\n\",\n    \"                        sides += 1\\n\",\n    \"                    # Check up\\n\",\n    \"                    if i == 0 or grid[i - 1][j] == 0:\\n\",\n    \"                        sides += 1\\n\",\n    \"                    # Check down\\n\",\n    \"                    if i == num_rows - 1 or grid[i + 1][j] == 0:\\n\",\n    \"                        sides += 1\\n\",","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/online_judges/island_perimeter/island_perimeter_solution.ipynb#L87-L123","documentation":"Raised by Solution.island_perimeter when grid is None. The method immediately does len(grid) and len(grid[0]), which would raise TypeError on None; the guard states the contract: a 2D list of 0/1 cells is required.","triggerScenarios":"Calling island_perimeter(None) — e.g. a matrix loader returned None, or grid came from a failed parse or an optional parameter.","commonSituations":"Reading grids from files/APIs that may be absent; tests exercising the None guard; notebooks where the cell defining the grid wasn't run.","solutions":["Pass a well-formed 2D list, e.g. [[0,1,0,0],[1,1,1,0],...]","Default the loader to [] or fail loudly when the grid source is missing","Ensure the notebook cell building the grid has executed before the call"],"exampleFix":"# before\nsolution.island_perimeter(grid)  # grid never assigned / loader returned None\n\n# after\ngrid = load_grid(path)\nif grid is None:\n    raise FileNotFoundError(path)\nsolution.island_perimeter(grid)","handlingStrategy":"validation","validationCode":"if grid is None: raise ValueError('grid is required')\nsolution.island_perimeter(grid)","typeGuard":"def is_grid(g): return isinstance(g, list) and all(isinstance(r, list) for r in g)","tryCatchPattern":"try:\n    p = solution.island_perimeter(grid)\nexcept TypeError as e:\n    raise ValueError('missing grid input') from e","preventionTips":["Run notebook cells that build inputs before using them","Have grid loaders fail loudly on missing sources"],"tags":["python","input-validation","none-check","matrix","grid"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}