java - 如何绕过 NullPointerException?

标签 java nullpointerexception gridworld

我正在尝试使用 eclipse 和 gridworld 检查奥赛罗游戏中的一步棋是否合法。我对位置所做的第一件事是检查它是否有效,但为了检查位置的有效性,它不能为空。问题是,它是合法移动的要求之一是它是空的/空的/无人占用的。我该如何避免这种情况?我已经指出了错误所在。 (抱歉,如果这让任何人感到困惑。)

public boolean isLegal(Location loc1)
{
    boolean isLegal = false;
    String currentColor = currentPlayer.getColor();
    int row = loc1.getRow();
    int col = loc1.getCol();
    if(board.isValid(loc1))
    {
        if(board.get(loc1) == null)
        {
            for(Location tempLoc : board.getValidAdjacentLocations(loc1))
            {
                **if(!board.get(tempLoc).equals(currentColor))**
                {
                    if((row != tempLoc.getRow()) && (col == tempLoc.getCol()))
                    {
                        //count up column
                        if(tempLoc.getRow() < row)
                        {
                            for(int i = row; i > 1;)
                            {
                                Location tempLoc2 = new Location(i-2, col);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i--;
                                }
                                else
                                {
                                    i=-1;
                                    isLegal = true;
                                }   
                            }
                        }
                        //count down column
                        else
                        {
                            for(int i = row; i < 6;)
                            {
                                Location tempLoc2 = new Location(i+2, col);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i++;
                                }
                                else
                                {
                                    i=9;
                                    isLegal = true;
                                }
                            }
                        }
                    }
                    else if(col != tempLoc.getCol() && row == tempLoc.getRow())
                    {
                        //count right row
                        if(col > tempLoc.getCol())
                        {
                            for(int i = col; i > 1;)
                            {
                                Location tempLoc2 = new Location(row, i-2);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i--;
                                }
                                else
                                {
                                    i=-1;
                                    isLegal = true;
                                }   
                            }
                        }
                        //count left row
                        else
                        {
                            for(int i = col; i < 6;)
                            {
                                Location tempLoc2 = new Location(row, i+2);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i++;
                                }
                                else
                                {
                                    i=9;
                                    isLegal = true;
                                }
                            }
                        }
                    }
                    else
                    {   //count up/right diag
                        if(row-1 == tempLoc.getRow() && col+1 == tempLoc.getCol())
                        {
                            int j = col;
                            for(int i = row; i > 1;)
                            {
                                Location tempLoc2 = new Location(i-1, j+1);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i--;
                                    j++;
                                }
                                else
                                {
                                    i=-1;
                                    isLegal = true;
                                }   
                            }
                        }
                        //count down/left diag
                        else if(row+1 == tempLoc.getRow() && col-1 == tempLoc.getCol())
                        {
                            int i = row;
                            for(int j = col; j > 1;)
                            {
                                Location tempLoc2 = new Location(i+1, j-1);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i++;
                                    j--;
                                }
                                else
                                {
                                    i=9;
                                    isLegal = true;
                                }   
                            }
                        }
                        //count up/left diag
                        else if(row-1 == tempLoc.getRow() && col-1 == tempLoc.getCol())
                        {
                            int j = col;
                            for(int i = row; i > 1;)
                            {
                                Location tempLoc2 = new Location(i-1, j-1);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i--;
                                    j--;
                                }
                                else
                                {
                                    i=-1;
                                    isLegal = true;
                                }   
                            }
                        }
                        //count down/right diag
                        else
                        {
                            int j = col;
                            for(int i = row; i > 6;)
                            {
                                Location tempLoc2 = new Location(i+1, j+1);
                                if(!board.get(tempLoc2).equals(currentColor))
                                {
                                    i++;
                                    j++;
                                }
                                else
                                {
                                    i=-1;
                                    isLegal = true;
                                }   
                            }
                        }
                    }
                }
            }
        }
    }
    return isLegal;
}

最佳答案

一种解决方案是更改您的设计,使任何位置都不会null

您似乎将 null 等同于“无人”或“空”。相反,首先创建所有位置(黑白棋盘上的位置并不多),并使用 boolean odds = false 或等效的成员变量初始化它们。然后你就会:

if ( !board.get(loc1).isOccupied() ) { /*stuff*/ }

而不是空检查。

这是更好的面向对象设计,因为空位置仍然是一个位置,并且应该是可操作的。

关于java - 如何绕过 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8874095/

相关文章:

java - 使用 Jsoup 登录

java - 将一维数组的内容传输到二维数组

java - 什么是NullPointerException,我该如何解决?

java - 在 GridWorld 的 GUI 中扩展一个类而不更改使用它的其他类

java - 我的扫描仪/终端窗口在重复一次后停止运行

java - 设置对象的颜色

java - 如何找出方法被执行?

java - 为什么我的 native C++ 代码在 Android 上的运行速度比 Java 慢得多?

java - 为什么我在 Android Studio 中不断收到 java.lang.nullpointerException?

java - 使用.getWidth()时无法定位空指针的原因