Need help using StdDraw to draw a 2D array

763 Views Asked by At

So I already have a method which reads a file which I have converted into a 2d array(therefore the dimensions of the array could be different each time depending on which file I use). Now I am trying to draw a rectangle using this and it wont work. My code is:

public static void drawBoard(String [][] board)  {

    for (int i = 0; (i < board.length); i++) { //line 88
        for (int j = 0; (j < board[0].length); j++) {
            int r = board.length;
            int c = board[0].length;
            double R = new Double(r);
            double C = new Double(c);

        StdDraw.setXscale(0,C);
        StdDraw.setYscale(0,R);
        StdDraw.setCanvasSize(500,500);
        StdDraw.setPenRadius(0.05);
        StdDraw.setPenColor(StdDraw.BLUE);
        StdDraw.rectangle((C/2), (R/2), (C/2), (R/2));
        StdDraw.close();


    }
}
}

What can I do to fix this? How can I get it to draw a rectangle? it says the error is in line 88 and that its a "java.lang.NullPointerException" error.

1

There are 1 best solutions below

4
Michael Kreutz On

StdDraw.rectangle expects input parameters of type double. You pass it input parameters of type int. This is ok but you might not want to do this. When calculating C/2 and R/2 you do integer division and thus you might accidentially round down. To fix this, you could initialize C and R as double.