Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7? -
this question has answer here:
exception in thread "main" java.lang.arrayindexoutofboundsexception: 7?
public static void main(string[] args) { piece[][] board; board = new piece[7][7]; // black pawns!create , set on board pawn bp1 = new pawn("black", true, 0, 6); board[0][6] = bp1; pawn bp2 = new pawn("black", true, 1, 6); board[1][6] = bp2; pawn bp3 = new pawn("black", true, 2, 6); board[2][6] = bp3; pawn bp4 = new pawn("black", true, 3, 6); board[3][6] = bp4; pawn bp5 = new pawn("black", true, 4, 6); board[4][6] = bp5; pawn bp6 = new pawn("black", true, 5, 6); board[5][6] = bp6; pawn bp7 = new pawn("black", true, 6, 6); board[6][6] = bp7; pawn bp8 = new pawn("black", true, 7, 6); board[7][6] = bp8; //line 93?? //white pawns!create , set on board pawn wp1 = new pawn("white", true, 0, 1); board[0][1] = wp1; pawn wp2 = new pawn("white", true, 1, 1); board[1][1] = wp2; pawn wp3 = new pawn("white", true, 2, 1); board[2][1] = wp3; pawn wp4 = new pawn("white", true, 3, 1); board[3][1] = wp4; pawn wp5 = new pawn("white", true, 4, 1); board[4][1] = wp5; pawn wp6 = new pawn("white", true, 5, 1); board[5][1] = wp6; pawn wp7 = new pawn("white", true, 6, 1); board[6][1] = wp7; pawn wp8 = new pawn("white", true, 7, 1); board[7][1] = wp8; //black rooks!create , set on board rook br1 = new rook("black", true, 0, 7); board[0][7] = br1; rook br2 = new rook("black", true, 7, 7); board[7][7] = br2; //white rooks!create , set on board rook wr1 = new rook("white", true, 0, 0); board[0][0] = wr1; rook wr2 = new rook("white", true, 7, 0); board[7][0] = wr2; //black knights!create , set on board knight bk1 = new knight("black", true, 1, 7); board[1][7] = bk1; knight bk2 = new knight("black", true, 6, 7); board[6][7] = bk2; //white knights!create , set on board knight wk1 = new knight("white", true, 1, 0); board[1][0] = wk1; knight wk2 = new knight("white", true, 6, 0); board[6][0] = wk2; //black bishops!create , set on board bishop bb1 = new bishop("black", true, 2, 7); board[2][7] = bb1; bishop bb2 = new bishop("black", true, 5, 7); board[5][7] = bb2; //white bishops!create , set on board bishop wb1 = new bishop("white", true, 2, 0); board[2][0] = wb1; bishop wb2 = new bishop("white", true, 5, 0); board[5][0] = wb2; //black king!create , set on board king bking = new king("black", true, 3, 7); board[3][7] = bking; //white king!create , set on board king wking = new king("white", true, 4, 0); board[4][0] = wking; //black queen!create , set on board queen bqueen = new queen("black", true, 4, 7); board[4][7] = bqueen; //white queen!create , set on board queen wqueen = new queen("white", true, 3, 0); board[3][0] = wqueen; }
i exception when i'm trying run project. there no error in built.
run: exception in thread "main" java.lang.arrayindexoutofboundsexception: 7 @ chess.startgame.main(startgame.java:93) java result: 1 build successful (total time: 0 seconds)
in java, array indexes range 0
length - 1
, in case, 0
through 6
. there no index 7
. have index 7
both dimensions, declare 2d array length 8
in both dimensions.
board = new piece[8][8];
Comments
Post a Comment