int grid[][] = new int[size_x][size_y];
You are declaring a new local grid
variable here, not initializing the field. Therefore, the grid
field remains null
and you receive an NPE when you try to index it. Try this instead:
grid = new int[size_x][size_y]; // use the field `grid`
As an aside, it's recommended in Java to put the []
of array declarations directly after the array type:
int[][] grid // good
int grid[][] // bad
You do this correctly when declaring the field, but not in the local variable declaration.