# LEETCODE- -NUMBER OF ISLANDS--RUNTIME ERROR

WHY MY CODE IS GIVING ERROR >>>Line 924: Char 9: runtime error: reference binding to null pointer of type ‘std::vector<char, std::allocator >’ (stl_vector.h)

``````class Solution {
public:

void dfs(vector<vector<char>> &grid,int i,int j,int n,int m)
{
if(i<0 || j<0|| i>=n||j>=m || grid[i][j]=='0'|| grid[i][j]=='v')
{
return ;
}

grid[i][j]='v';
dfs( grid, i+1, j, n, m);
dfs( grid, i-1, j, n, m);
dfs( grid, i, j+1, n, m);
dfs( grid, i, j-1, n, m);

}

int numIslands(vector<vector<char>>& grid)
{
int n=grid.size();
int m=grid[0].size();
int ans=0;
for(int i=0;i<n;i++)
{  for(int j=0;j<m;j++)
{
if( grid[i][j]=='1')
{
ans++;
dfs( grid, i, j, n, m);
}

}
}
return ans;
}

};

I’m not familiar with this runtime error message, but a good trick when the compiler actually tells you something is to look up your exact error - chances are someone else has also faced it before.

1 Like

your void dfs function is wrong…that why you get runtime error…
try this …
( you can replace grid.size()by n and grid[0].size()by m)

``````void dfs(vector<vector<char>>& grid,int i,int j){
grid[i][j]='0';
if(i-1>=0 && grid[i-1][j]=='1'){
dfs(grid,i-1,j);
}
if(j-1>=0 && grid[i][j-1]=='1'){
dfs(grid,i,j-1);
}
if(i+1<grid.size() && grid[i+1][j]=='1'){
dfs(grid,i+1,j);
}
if(j+1<grid[0].size() && grid[i][j+1]=='1'){
dfs(grid,i,j+1);
}

}
``````

if still have problem then watch this solution video
explanation

1 Like

thank you i spotted the error >>>the value of columns can be diffrent i shoold have iterated j from 0 tiogrid [i].size() not grid[0].size() …