简体   繁体   中英

Why am I receiving an ArrayIndexOutofBoundsException when trying to check for mills?

I'm working on a nine men morris GUI and the method I'm currently stuck on is checking for mills. I understand that the pieces are outside bounds however shouldn't it just return false and work fine anyways? How do I go about checking for pieces that could potentially be outside of the board?

private boolean isMill(ActionEvent e) {
        
        for(int r = 0; r < this.board.length; r++) {
            for(int c = 0; c < this.board[r].length; c++) {
                
                if(this.board[r][c] == e.getSource()) {
                    
                    JButton piece = this.board[r][c];
                    
                    JButton abovePiece = this.board[r + 1][c];
                    JButton belowPiece = this.board[r - 1][c];
                    JButton rightPiece = this.board[r][c + 1];
                    JButton leftPiece = this.board[r][c - 1];
                    
                    JButton afterAbovePiece = this.board[r + 2][c];
                    JButton afterBelowPiece = this.board[r - 2][c];
                    JButton afterRightPiece = this.board[r][c + 2];
                    JButton afterLeftPiece = this.board[r][c - 2];
                    
                    if(this.isBlack(piece)){
                        
                        if(this.isBlack(abovePiece) && this.isBlack(afterAbovePiece)) {
                            return true;
                        }
                        
                        if(this.isBlack(belowPiece) && this.isBlack(afterBelowPiece)) {
                            return true;
                        }
                        
                        if(this.isBlack(rightPiece) && this.isBlack(afterRightPiece)) {
                            return true;
                        }
                        
                        if(this.isBlack(leftPiece) && this.isBlack(afterLeftPiece)) {
                            return true;
                        }
                        
                        if(this.isBlack(abovePiece) && this.isBlack(belowPiece)) {
                            return true;
                        }
                        
                        if(this.isBlack(leftPiece) && this.isBlack(rightPiece)) {
                            return true;
                        }
                        
                        // TODO: Same thing but for whtie pieoces
                        
                    }
                    
                }
                
            }
            
        }   
        
        return false;
        
    }

No, it shouldn't just return false. Exceptions in Java are events that disrupts the normal flow of the program's instructions, and with no exception handler that can handle this kind of error, the runtime system will abort the program with the exception message printed to the console.

Wrap your method with a try-catch block, to catch the exception when it's thrown and handle it in some way that will gurarantee that your program can keep running normally.

For further reading about Java's Exceptions: The Java™ Tutorials: What is an Exception?

when r is equals to this.board.length-1,this.board[r + 1] will give u an ArrayIndexOutofBoundsException. same to the param c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM