Hi there:
This seems like an interesting problem. The array pos is defined within class. But the object is actual created inside the scope of a method. Seems like this object goes out of scope when control goes out of this method. So I modified your code and declare and created the array object pos within class (outside of method). And did not get nullpointererror. Note, I have modified your code little bit so that I can see output on one screen. Here is the code:
class Board {
private int size;
private int[][] pos = new int[2][2];
public Board(int s) {
size = s;
// int[][] pos = new int[s][s];
for(int i=0; i<s; i++)
for(int j=0; j<s; j++) {
pos[i][j] = 0;
System.out.println(i+" "+j+" "+pos[i][j]);
}
setupBoard();
System.out.println("2!!!");
}
public void setupBoard() {
for (int i = 0; i!=size; i++)
for (int j = 0; j!=size; j++) {
if (i < size/2 ) {
System.out.println(i+" "+j+" "+pos[i][j]);
pos[i][j]=j+1;
System.out.println(i+" "+j+" "+pos[i][j]);
}
if (i> (size - size/4)) {
System.out.println("!!!");
pos[i][j]=(j+1) * -1;
}
}
}
}
public class Board_test {
public static void main(
String[] args) {
Board B = new Board(2);
}
}